使用Powershell根据文件名创建文件夹

rkkpypqq  于 2023-02-04  发布在  Shell
关注(0)|答案(1)|浏览(275)

我需要有关创建文件夹的帮助。我正在提取Azure订阅的报告,这些报告保存在多个txt文件中,例如1App.txt、2App.txt。提取的报告保存在一个文件夹名称输出中。
我需要根据txt文件的名称创建一个文件夹,并将输出文件夹中的文件移动到新创建的文件夹中。还可以设置一个条件,如果订阅的是1App.txt,则保存在输出中的文件应移动到1App文件夹中。还需要设置一个条件,以便它应该能够移动文件到现有的文件夹时,脚本再次运行。

$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
    }
}
umuewwlo

umuewwlo1#

使用Test-PathNew-Item有条件地创建以文件的基本名称命名的文件夹:

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
}

相关问题