在这个问题上肯定有一些明显的地方我做错了,但我需要另一双眼睛来帮助我,因为我没有遇到任何错误 - 它只是没有做我想做的事......我需要将软件安装程序文件推送到“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"}}
我得到的结果显示了 servers.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”确保唯一的结果是应该在写主机行。
再一次,我觉得这是很明显的事情,但我正在把头发扯下来弄清楚。