ドメインに参加しているすべてのコンピューターにライセンス ファイルをコピーしようとしていますが、PowerShell しか使用できません。GPO または (GPO) スケジュールされたタスクでさえ、Server 2003 環境内ではまだ実行できないように見えるため、実行できません。これに関するアプリケーションは既にインストールされており、ライセンス ファイルのみを上書きする必要があります。
私は達成したいと思っています: - オンラインかどうかを確認し、そうでない場合はスキップします - 32b フォルダーの場所を確認し、存在する場合はコピーし、そうでない場合はスキップします - 64b フォルダーの場所を確認し、存在する場合はコピーし、そうでない場合はスキップします - すべてのコンピューターの結果をログ ファイルに書き込みますプルーニングを成功させることができるように
最初の試行;
私が現在持っているコード:
$computers = gc "c:\temp\computers.txt" $source = "c:\temp\almmodule.lic" $dest32b = 'c$\program files (x86)\ALM - Automatic Login Module' $dest64b = 'c$\program files\ALM - Automatic Login Module' $TestPath32 = Test-Path -path "\\$computer\$dest32b\*" $TestPath64 = Test-Path -path "\\$computer\$dest64b\*" foreach ($computer in $computers) { if (test-Connection -Cn $computer -quiet) { IF (!$TestPath32) { Copy-Item $source -Destination "\\$computer\$dest32b" -recurse -force -verbose} ELSE { "$computer' 32b folder not found. Skipping"} IF (!$TestPath64) { Copy-Item $source -Destination "\\$computer\$dest64b" -recurse -force -verbose} ELSE {"$computer 64b folder not found. Skipping"} ELSE { "$computer is not online" } } }
いくつかの小さなバリエーションを試しましたが、どこにも行けないようです。また、ロギングはまだ作成する必要があります。
自分自身をテストターゲットとして使用し、変数を超えて実行し、単一のコマンドを使用します。
$TestPath32 = テストパス -パス "\$computer\$dest32b*"
戻り値: 真
Copy-Item $source -Destination "\$computer\$dest32b" -recurse -force -verbose
成功し、ファイルをコピーします
現時点で PowerShell を実行すると、最後の ELSE ステートメントについて不平を言います。
しかし、ほとんどの場合、64b フォルダーがないことを認識できず、エラーが発生するか、ディレクトリをファイル名としてファイルが配置されます。
私は途方に暮れており、今では多くのことを試してきましたが、私が持っている小さなことにブレーキをかけるのが怖いです.
2 回目の試行。
コードを編集し、いくつかの部分をコメントアウトして、そこから作業モデルを作成しました。
foreach ($computer in $computers) { $TestPath32 = Test-Path "\\$computer\$dest32b\*" $TestPath64 = Test-Path "\\$computer\$dest64b\*" # if (test-Connection -Cn $computer -quiet) { IF (!$TestPath32) { Copy-Item $source -Destination "\\$computer\$dest32b\" -recurse -force -verbose} ELSE {"$computer' 32b folder not found. Skipping"} IF (!$TestPath64) { Copy-Item $source -Destination "\\$computer\$dest64b\" -recurse -force -verbose} ELSE {"$computer 64b folder not found. Skipping"} # ELSE { # "$computer is not online" # } #} }
戻り値:
PS C:\windows\system32> C:\Temp\ALM 2016 LIC copy.ps1 L2016010' 32b folder not found. Skipping VERBOSE: Performing operation "Copy File" on Target "Item: C:\temp\almmodule.lic Destination: \\L2016010\c$\program files\ALM - Automatic Login Module\". Copy-Item : De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist. At C:\Temp\ALM 2016 LIC copy.ps1:14 char:12 + Copy-Item <<<< $source -Destination "\\$computer\$dest64b\" -force -verbose} + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
32b パスがあることを保証できます。copy-item はファイルを配置しません。私は明らかに64bパスを持っていません.$falseをきちんと返すのではなく、これで壊れていると感じています。
パスをいじると (それが失敗の原因だと思ったので)、ライセンス ファイルのサイズである「ALM - 自動ログイン モジュール」という名前のファイルがプログラム ファイルに配置されることがあります。
繰り返しますが、ライン
Copy-Item $source -Destination "\\$computer\$dest32b\"
をスタンドアロンとして実行すると、ファイルがコピーされます。
3回目の試行、現在機能しています。
$computers = gc "c:\temp\computers.txt"
$source = "c:\temp\almmodule.lic"
$dest32b = 'c$\program files (x86)\ALM - Automatic Login Module'
$dest64b = 'c$\program files\ALM - Automatic Login Module'
foreach ($computer in $computers) {
$TestUp = test-Connection -Cn $computer -quiet
$TestPath32 = Test-Path -pathType container "\\$computer\$dest32b"
$TestPath64 = Test-Path -pathType container "\\$computer\$dest64b"
IF (!$TestUp) {
write-host "$computer is not online"
} ELSE {
IF (!$TestPath32){
write-host "$computer' 32b folder not found. Skipping"
} ELSE {
Copy-Item $source -Destination "\\$computer\$dest32b\" -force -verbose
}
IF (!$TestPath64){
write-host "$computer 64b folder not found. Skipping"
} ELSE {
Copy-Item $source -Destination "\\$computer\$dest64b\" -force -verbose
}
}
}
!$ の使用は完全に私の頭の中にあり、次の点から作業することになっていました。
そうでない場合は、そうでなければ
現在、スクリプトは存在しないフォルダーをスキップします。ダウンしたコンピューターをまだテストできていませんが、これも機能すると思います。
今私が理解する必要があるのは、出力を1つのログファイルに読み取り可能な形式で記録する方法です