0

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

再一次,我觉得这是很明显的事情,但我正在把头发扯下来弄清楚。

4

1 回答 1

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"
    }
}

希望有帮助

于 2018-10-06T10:21:56.580 回答