使用PowerShell在远程服务器上添加文件夹

时间:2018-10-05 17:16:09

标签: powershell directory new-item

在这个地方肯定有明显的地方我在做错事,但是我需要另一只眼睛来帮助我,因为我没有遇到任何错误-它只是没有按照我的意愿去做...我需要将软件安装程序文件推送到“ X:\ folder \ Software Install Files”,并将现有文件备份到一堆不同的服务器上的“ X:\ folder \ Software Backups”。为此,我需要确保首先存在这些文件夹,如果不存在则创建它们。下面的脚本从servers.txt文件中收集$ computer变量,然后针对每台计算机,我查看注册表以找出软件当前安装在哪个驱动器上,然后在驱动器上创建适当的文件夹(如果不存在) :

# This file contains the list of Servers
$computers = gc "C:\Folder\Subfolder\Servers.txt"

clear-host

# The Command below pulls all the variables above and performs the file copy
foreach ($computer in $computers) {

#Gather Version and Program Directory Information from Registry


    $machinename = $computer
    $icakey = "SOFTWARE\\Wow6432Node\\Program\\Program"
    $ICAVers = "Version"
    $ICADrive = "InstallDir"

    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', 
$machinename)
    $icaregkey = $reg.opensubkey($icakey)
    $ICAV = $icaregkey.getvalue($IcaVers)
    $ICAD = $icaregkey.getvalue($IcaDrive)

#Declare Directory Drive Letter As Variable

    $DriveLetter,$Folder = $ICAD.split(":")

#Declare path for folders

    $Softwarepath = "\\$computer\$driveletter$\Folder\Software Install Files"
    $BackupPath = "\\$computer\$driveletter$\Folder\Software Backups"

#Create Software Folder

    If(test-Path -path $SoftwarePath)
        {Write-host "$SoftwarePath already exists on $computer" -ForegroundColor Red}
        ELSE
            {{New-Item -Path $SoftwarePath -ItemType Directory}
                {write-host "$SoftwarePath created on $computer"}}

#Create WebAccess Backups Folder

    If(test-Path -path $BackupPath)
        {Write-host "$BackupPath already exists on $computer" -ForegroundColor Red}
        ELSE
            {{New-Item -Path $BackupPath -ItemType Directory}
                {write-host "$BackupPath created on $computer"}}

我得到的结果显示了server.txt中每台服务器的结果:

New-Item -Path $SoftwarePath -ItemType Directory
write-host "$SoftwarePath created on $computer"
New-Item -Path $BackupPath -ItemType Directory
write-host "$BackupPath created on $computer"

它实际上不是在创建文件夹,在以前的时候我做过类似的事情,结果中的变量显示了它们代表的值,并且顶部的“ clear-host”确保唯一的结果是应该在写主机行中的内容。

再次,我觉得这很明显,但是我正在撕掉头发以弄清楚。

1 个答案:

答案 0 :(得分:0)

为避免在注释中进行长时间讨论,我想我可能已经发现您的代码中缺少的内容。实际上,它与您为$Softwarepath$BackupPath设置的路径有关,在这些路径中您忘记了$之前的Folder符号。目录可能是创建的,但不是您期望的位置。他们将以\\computername\X$\Folder\Software Install Files结尾。

无论如何,这是您代码的修订版:

# This file contains the list of Servers
$computers = Get-Content "C:\Folder\Subfolder\Servers.txt"

clear-host

# The Command below pulls all the variables above and performs the file copy
foreach ($computer in $computers) {

    #Gather Version and Program Directory Information from Registry
    $machinename = $computer
    $icakey   = "SOFTWARE\\Wow6432Node\\Program\\Program"
    $ICAVers  = "Version"
    $ICADrive = "InstallDir"

    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $machinename)
    $icaregkey = $reg.opensubkey($icakey)
    $ICAV = $icaregkey.getvalue($IcaVers)
    $ICAD = $icaregkey.getvalue($IcaDrive)

    #Close the registry keys when done
    $icaregkey.Close()
    $reg.Close()

    #Declare Directory Drive Letter As Variable
    $DriveLetter,$Folder = $ICAD.split(":")

    #Declare path for folders
    $Softwarepath = "\\$computer\$driveletter$\$Folder\Software Install Files"
    $BackupPath   = "\\$computer\$driveletter$\$Folder\Software Backups"

    #Create Software Folder
    if(Test-Path -Path $SoftwarePath -PathType Container) { 
        Write-Host "$SoftwarePath already exists on $computer" -ForegroundColor Red
    }
    else {
        New-Item -Path $SoftwarePath -ItemType Directory -Force
        Write-Host "$SoftwarePath created on $computer"
    }

    #Create WebAccess Backups Folder
    if(Test-Path -Path $BackupPath -PathType Container) {
        Write-Host "$BackupPath already exists on $computer" -ForegroundColor Red
    }
    else {
        New-Item -Path $BackupPath -ItemType Directory -Force
        Write-Host "$BackupPath created on $computer"
    }
}

希望有帮助