I am new to Powershell and trying to achieve the following.
I have multiple .txt file with multiple Azure subscriptions. Each text file is for one application which is like ABC.txt, A1B.txt.
The output is saved in .csv. I need to name the .csv file as per input .txt file. When the script is running for ABC.txt output should be saved as ABC.csv along with date. Any help please. Below is my complete code where the files are getting saved only with date stamp.
$Folder = 'C:\Scrpting\Subscriptions'
$Files = Get-ChildItem -Path $Folder -File | Select-Object -ExpandProperty FullName
$FileName =
$AzSubs = Get-Content -Path $Files
$AzSubs
$Result = ForEach ($AzSub in $AzSubs) {
Set-AzContext -SubscriptionName $AzSub
$Userroles = Get-AzRoleAssignment | Where-Object -FilterScript {$_.ObjectType -eq "User"}
Foreach ($User in $Userroles) {
$info = "" | Select "DisplayName", "SignInName", "RoleDefinitionName", "SubscriptionName"
$info.DisplayName = $User.DisplayName
$info.SignInName = $User.SignInName
$info.RoleDefinitionName = $User.RoleDefinitionName
$info.SubscriptionName = $AzSub
$info | Export-Csv C:\Scrpting\Output\UserRoles$((Get-Date).ToString("yyyyMMdd")).csv -Append}
}
ForEach ( $azSub in $azSubs ) {
Set-AzContext -SubscriptionName $azSub
$azNsgs = Get-AzNetworkSecurityGroup
$Output = ForEach ( $azNsg in $azNsgs ) {
#Export custom rules
Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg | `
Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, `
@{label = 'NSG Location'; expression = { $azNsg.Location } }, `
@{label = 'Rule Name'; expression = { $_.Name } }, `
@{label = 'Source'; expression = { $_.SourceAddressPrefix } }, `
@{label = 'Source Application Security Group'; expression = {$_.SourceApplicationSecurityGroups.id.Split('/')[-1] } },
@{label = 'Source Port Range'; expression = { $_.SourcePortRange } }, Access, Priority, Direction, `
@{label = 'Destination'; expression = { $_.DestinationAddressPrefix } }, `
@{label = 'Destination Application Security Group'; expression = { $_.DestinationApplicationSecurityGroups.id.Split('/')[-1] } }, `
@{label = 'Destination Port Range'; expression = { $_.DestinationPortRange } }, `
@{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } },
@{label = 'Subscription Name'; expression = {$azSub} }
}
$Output | Export-Csv -Path C:\Scrpting\Output\FirewallRules$((Get-Date).ToString("yyyyMMdd")).csv -Append
}
ForEach($AzSub in $AzSubs) {
# change azure subscription
Set-AzContext -SubscriptionID $AzSub
$vms = Get-AzVM
$vmrg = Get-AzVM | Select-Object "ResourceGroupName"
$nics = get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
ForEach($nic in $nics) {
$info = "" | Select VMName, ResourceGroupName, OS, PrivateIPAddress, PublicIPAddress, SubscriptionID, Status, NICName
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
$info.NICName = $nic.Name
$info.VMName = $vm.Name
$info.SubscriptionID = $subscriptionId
$info.ResourceGroupName = $vm.ResourceGroupName
$info.PrivateIPAddress = $nic.IpConfigurations.PrivateIpAddress
$PublicIPAddress = (Az vm list-ip-addresses --name $vm.Name --resource-group $vm.ResourceGroupName | ConvertFrom-Json).virtualMachine.network.publicIpAddresses.ipaddress
$info.PublicIPAddress = if ($PublicIPAddress -eq $null) {"Not Assigned"} else {$PublicIPAddress}
$info.OS = $vm.StorageProfile.osDisk.osType
$info.Status = ((Get-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status).Statuses[1]).code
$info| Export-Csv -Path C:\Scrpting\Output\VMInfo$((Get-Date).ToString("yyyyMMdd")).csv -Append
}
}
CodePudding user response:
At the moment $AzSubs
contains the contents of all your files; but you've already lost the reference to the parent filename.
$Folder = 'C:\Scrpting\Subscriptions'
$Files = Get-ChildItem -Path $Folder -File | Select-Object -ExpandProperty FullName
$AzSubs = Get-Content -Path $Files
Change the above to this, so you keep that filename reference
$Folder = 'C:\Scrpting\Subscriptions'
# Note: I've included a filter for *.txt since our CSVs are being output to the same path / we don't want to accidentally read those in on a future run
$Files = Get-ChildItem -Path $Folder -File -Filter '*.txt' | Select-Object -ExpandProperty FullName
foreach ($filename in $Files) {
# here we only take the contents of the single file for the subscription, rather than for all files.
# NOTE: I'm assigning directly to `$AzSub`; we don't need `$AzSubs` anymore.
$AzSub = Get-Content -Path $filename
# we can also get a CSV version of our filename by replacing the .txt on the end of the source filename with whatever we need (e.g. date and csv extension)...
$csvFilename = $filename -replace '.txt$', ('_{0:yyyyMMdd}.csv' -f (Get-Date).ToUniversalTime())
# the rest of your code goes here
}