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

Powershell 在远程计算机上的不同ForEach中嵌入ForEach语句,powershell,remote-access,Powershell,Remote Access,和往常一样,我对powershell还很陌生,并试图自学。提前感谢大家: 我们有一个登录脚本,可以自动将注册表设置到我们的主页。在构建计算机时,我们将登录脚本放在C:\Users\Default\%Appdata%\roaming….\startup\文件夹中。这样,任何登录的新用户都会将bat文件放入其%AppData%文件夹中,并自动设置其主页 我们最近建立了一个新的服务器,由于一些问题,我们需要更改我们的主页URL,因此需要更改所有计算机上所有用户配置文件的logon.bat文件 我在这

和往常一样,我对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($PC in$Computers){……}语句中,但是遇到了所有这些奇怪的问题,它只会影响运行脚本的本地机器。例如,获取我的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
        }
    }
    } 

如何将这两个脚本组合起来:
对于我列表中的每台计算机,查看所有用户配置文件,并为每个配置文件(不包括提及的配置文件)添加新登录。bat

如果您在顶部发布的脚本对您有效,并且只要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
        }
    }
}
然后使用您的foreach循环,如下所示:

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

foreach($pc in $computers){
    Invoke-Command -ComputerName $pc -ScriptBlock $scriptblock
}

我们的一位主要程序员问道,他纠正了我的原始脚本,而不需要添加scipt块。谢谢你的第一个建议

这是最后一个成功的脚本

$source = '\\ITE00463866\Applications\_Layer1_Installs\TS Sector\firstlogon.bat'
$list = "\\ITE00463866\Applications\_Layer1_Installs\TS Sector\computers.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')
$profilesfolder = '\\' + $pc + '\c$\users\'
$profiles = get-childitem $profilesfolder -Directory -force | Where-Object { $_.BaseName -notin $excluded_profiles }


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

感谢Sid,但是,是的,我们的WinRM没有配置为运行脚本块。。由于WinRM错误而停止。我问过我们的网络技术人员是否有任何解决办法或获得例外。。。