在远程计算机上的不同ForEach中嵌入ForEach语句

问题描述 投票:0回答:1

与往常一样,PowerShell的新手,并试图自学。预先谢谢大家:

我们有一个登录脚本,可将注册表自动设置为我们的主页。在构建计算机时,我们将登录脚本放在C:\ Users \ Default \%Appdata%\ roaming .... \ startup \文件夹中。这样,任何新登录用户都会将bat文件放入其%AppData%文件夹中,并自动设置其主页。

我们最近构建了新服务器,由于某些问题,我们需要更改我们的主页URL,因此需要为所有用户配置文件在所有计算机上更改logon.bat文件。


我在这里找到的此脚本运行完美,但仅在运行该脚本的本地计算机上:

$source = '\\ITE00463866\Applications\_Layer1_Installs\TS Sector\firstlogon.txt'
$profilesfolder = 'c:\users\'
$excluded_profiles = @( 'All Users', 'Default User', 'Default.migrated', 'Public', 'DefaultAppPool', 'cdwuser', '.NET v4.5 Classic', '.NET v4.5')
$profiles = get-childitem $profilesfolder -Directory -force | Where-Object { $_.BaseName -notin $excluded_profiles }
$targetfolder = "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

foreach ($profile in $profiles) {
    $destination = $profilesfolder + $profile + $targetfolder
    If ( $(Try { Test-Path $destination.trim() } Catch { $false }) ) {

        copy-item -path $source -destination $destination -Force -Verbose
        }
    Else {

       New-Item -Path $destination -ItemType Directory
       copy-item -path $source -destination $destination -Force -Verbose
        }
    } 

我一直在尝试将上述ForEach语句添加到Get-Content |中。 FOREACH($ Computers中的$ PC){....}语句,但是会收到所有这些ODD问题,并且仅影响运行脚本的本地计算机。例如,获取我的System32文件夹中的每个文件夹并创建一个名为System32文件夹命名的用户,然后将logon.bat放在所有这些%AppData%文件夹中...

 $source = '\\ITE00463866\Applications\_Layer1_Installs\TS Sector\firstlogon.txt'
$list = "\\ITE00463866\Applications\_Layer1_Installs\TS Sector\test.txt"
$computers = gc $list

foreach($pc in $computers){

$targetfolder = "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
$excluded_profiles = @( 'Administrator', 'All Users', 'Default User', 'Default.migrated', 'Public', 'DefaultAppPool', 'cdwuser', '.NET v4.5 Classic', '.NET v4.5')
$profiles = get-childitem $profilesfolder -Directory -force | Where-Object { $_.BaseName -notin $excluded_profiles }
$profilesfolder = 'c:\users\'

foreach ($profile in $profiles) {

    $destination = $profilesfolder + $profile + $targetfolder
       if ( $(Try { Test-Path $destination.trim() } Catch { $false }) ) {

       #If folder Startup folder is found for profile, add file to destionation, force overwrite
        copy-item -path $source -destination $destination -Force -Verbose
        }
    Else {

       #If folder is NOT found, create folder and move file to destination
        New-Item -Path $destination -ItemType Directory
        copy-item -path $source -destination $destination -Force -Verbose
        }
    }
    } 

我如何将两个脚本结合到:对于列表中的每台计算机,请在所有用户配置文件中查找,并为每个配置文件(提及的配置文件除外)添加新的logon.bat

powershell remote-access
1个回答
0
投票

如果您在顶部发布的脚本对您有用,并且只要在您的环境中配置了winrm,您就可以将其封装在如下所示的脚本块中:

$scriptblock = {
    $source = '\\ITE00463866\Applications\_Layer1_Installs\TS Sector\firstlogon.txt'
    $profilesfolder = 'c:\users\'
    $excluded_profiles = @( 'All Users', 'Default User', 'Default.migrated', 'Public', 'DefaultAppPool', 'cdwuser', '.NET v4.5 Classic', '.NET v4.5')
    $profiles = get-childitem $profilesfolder -Directory -force | Where-Object { $_.BaseName -notin $excluded_profiles }
    $targetfolder = "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

    foreach ($profile in $profiles) {
    $destination = $profilesfolder + $profile + $targetfolder
    If ( $(Try { Test-Path $destination.trim() } Catch { $false }) ) {

        copy-item -path $source -destination $destination -Force -Verbose
        }
    Else {

        New-Item -Path $destination -ItemType Directory
        copy-item -path $source -destination $destination -Force -Verbose
        }
    }
}

然后使用ur foreach循环,如下所示:

$list = "\\ITE00463866\Applications\_Layer1_Installs\TS Sector\test.txt"
$computers = gc $list

foreach($pc in $computers){
    Invoke-Command -ComputerName $pc -ScriptBlock $scriptblock
}
© www.soinside.com 2019 - 2024. All rights reserved.