以下のタスクを実行するには、以下のスクリプトが必要です。 ADからサーバーのリストを取得し、そのサーバー名のそれぞれを繰り返し、ディレクトリ内の最新のフォルダを取得して名前を変更し、別のサーバーにコピーする必要があります。Powershell:ForEachループでコピーアイテムが機能しない
私はforeachループでそれを持っている場合は、以下書かれたコピー-Itemコマンドは、動作していない:書かれたとして、私は、foreachループの外でそれをテストしていた場合しかし、それは正常に動作します
#gathering server names
$serverList = (Get-ADComputer -Filter "Name -like 'Q0*00*'" -SearchBase "OU=MPOS,OU=Prod,OU=POS,DC=N,DC=NET").name | Sort-Object | Out-File C:\Temp\MPOS\MPOSServers.txt
$serverListPath = "C:\Temp\MPOS\MPOSServers.txt"
#Retrieve a list of MPOS Print servers from text file and set to $serverNames
$serverNames = Get-Content -Path $serverListPath
#Iterate through each of the server names
foreach ($serverName in $serverNames) {
$reportServer = "a03"
Get-ChildItem "\\$($serverName)\d$\MPosLogs\Device" |
Where { $_.PSIsContainer } |
Sort CreationTime -Descending |
Select -Skip 1 |
Select -First 1 |
ForEach-Object {
Rename-Item -Path $_.FullName -NewName ("$serverName" + "_" + $_.Name) -PassThru |
Copy-Item -Destination "\\$($serverName)\c$\temp\MPOS\Logs"
}
}
以下:
Get-ChildItem "\\$($serverName)\d$\MPosLogs\Device" |
Where { $_.PSIsContainer } |
Sort CreationTime -Descending |
Select -Skip 1 |
Select -First 1 |
ForEach-Object {
Rename-Item -Path $_.FullName -NewName ("$serverName" + "_" + $_.Name) -PassThru |
Copy-Item -Destination "\\$($serverName)\c$\temp\MPOS\Logs"
}
なぜ完全なスクリプトで動作していないのですか?私はそれをテストするときに私は何も変更していない、私はちょうど上記のコマンドを実行していないForEachループにある。フォルダのコピーを除いて、残りのタスクは完了です。フォルダーコピーは、単一サーバー上のForEachループ外でテストする場合にのみ機能します。
「うまくいきません」と言えば、エラーはありません。単にフォルダをコピーしていないだけです。
ありがとうございました! :) LG
私はこのコードを覚えています:) '$ ServerListPath'は有効なファイル権限を指していますか? 'write-host $ servernames'の結果を表示できますか?その配列がどのように生成されているかにいくつかの問題があると思われます。 –
@MikeGaruccio hello :)はい新しい問題の新しいスレッドを開始したいと思います。 Write-Host $ serverNamesは、この形式のすべてのサーバー名を一覧表示します。Q0001001 Q0002001 Q0003001 - – LilithGoddess
'$ serverNames.Count'は何を表示しますか? –