I need help in creating folder. I am pulling up report for Azure subscription which are saved in multiple txt files Eg. 1App.txt, 2App.txt. The report pulled are saved in one folder name Output.
I need to create an folder as per the name of txt files and move files of Output folder to the newly created folder. Also is it possible to put a condition that if subscription is of 1App.txt then files saved in Output should be moved to 1App folder. Also need to put a condition so that it should be able to move files to existing folder when the script is run again.
$Folder = 'C:\Scrpting\Subscriptions'
$Files = Get-ChildItem -Path $Folder -File | Select-Object -ExpandProperty FullName
$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:
Use Test-Path
and New-Item
to conditionally create a folder named for the file's basename:
foreach($file in Get-ChildItem -File){
# test if folder exists
if(-not(Test-Path $file.BaseName)){
# if not, create it!
New-Item -Name $file.BaseName -ItemType Directory
}
# move the file
$file |Move-Item -Destination $file.BaseName
}