2016-12-12 9 views
2

ドメインに参加しているすべてのコンピュータにライセンスファイルをコピーしようとしています。 、PowerShellのみを使用できます。 GPOまたはスケジュールされた(GPO)スケジュールされたタスクは、Server 2003環境内ではまだ可能ではないように見えるため、スケジュールされていません。 これに関するアプリケーションは既にインストールされており、ライセンスファイルのみを上書きする必要があります。複数のチェックを含むドメインコンピュータへのPowerShell一括コピー

私は達成するために期待しています: - 他 をスキップし、存在する場合、32bのフォルダの場所のためのコピーをチェック - - をスキップ他、オンラインかどうかを確認、64bのフォルダの場所を確認するコピーを存在する場合、他の をスキップ - 書きますすべてのコンピュータの結果がログファイルに出力されるので、成功することができます。

1回目の試み。私が現在持っている

コード:

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

私はいくつかのマイナーなバリエーションを試してみたが、私はどこに行くように見えることはできません。 ロギングも作成する必要があります。

自分自身をテストターゲットとして使用し、上記の変数を実行し、単一のコマンドを使用します。 "\ $コンピュータの\ $のdest32bは*"

戻り-path

$ TestPath32 =テストパス:真

コピー項目$源-Destination "\ $コンピュータ\ $ dest32b" -recurse - 力は-verbose

は成功し、コピー現時点でPowerShellを実行して、ファイル

は、最後のELSE文の文句を言います。

しかし、ほとんどの場合、私は64bフォルダを持っていないことに気づきませんでした。そして、それはエラーを出したり、ディレクトリをファイル名として置いたりします。

私は喪失しています。私は今、私が持っている小さなものを制動するのを恐れています。


第二の試み。

私はこのコードを編集し、いくつかの部分をコメントアウトしました。そこから進歩するための作業用モデルを得るためです。

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パスを持っていますかを保証することができます。コピーアイテムはファイルを配置しません。 私は明らかに64bパスを持っていません、そして、私はそれがちょうどきちんと$ falseを返すのではなく、これを破ると感じます。

彼のパス(私はそれが失敗の原因だと思ったので)で、私は混乱arroundの、それは時々名前のプログラムファイルでファイル置く - ライセンスファイルのサイズである「ALM自動ログインモジュール」 。

また、Copy-Item $source -Destination "\\$computer\$dest32b\"をスタンドアロンとして実行すると、ファイルをコピーします。今働いて


第三の試み;

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

の使用$は完全に私の頭の上に行って、私がした時点から動作するようになっていた!

今、スクリプトが存在しないフォルダをスキップしない場合は、THEN、ELSE

、ダウンコンピュータをまだテストすることはできませんでしたが、私は今これも動作すると仮定します。

今私が把握する必要があるすべては、1つのログファイルに出力をログに記録する方法です、コメントで述べたように、私は、同様GPOに投票読める形式

+0

をGPO許可されていない理由として、実際のソリューション?これは、新しいコンピュータがオンラインになるたびにライセンスファイルを手動でインストールする必要がないため(または古いものを再構築するなど)、最も適切なソリューションと思われます。 –

+0

[File Item](https://technet.microsoft.com/en-us/library/cc772536(v = ws.11).aspx)オプションは、これを行うためのより良い方法です。あなたは望み、x86/x64を扱うためにフィルタリングすることができます。 –

+0

GPOは実際には不可能です。 Windows 2003は、私が見ている限り、そのような種類のGPOをまだサポートしていません。 –

答えて

0

最終バージョン。 私がしたいようにファイルを出力できませんでした。

スタート - トランスクリプトに合格しました。 Get-Contentを最後に置くと、出力を読み込み可能な形式に整形する必要がありました。

#set-executionpolicy RemoteSigned 
$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' 
Start-Transcript -path C:\temp\ALM-Log.txt -append 
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`r" 
    } ELSE { 
     IF (!$TestPath32){ 
     write-host "$computer' 32b folder not found. Skipping`r" 
    } ELSE { 
     Copy-Item $source -Destination "\\$computer\$dest32b\" -force -verbose 
     } 
     IF (!$TestPath64){ 
     write-host "$computer 64b folder not found. Skipping`r" 
    } ELSE { 
     Copy-Item $source -Destination "\\$computer\$dest64b\" -force -verbose 
     } 
    } 
} 
Stop-Transcript 

$log = Get-Content C:\temp\ALM-Log.txt 
$log > c:\temp\ALM-Log.log 

この作業の理由= Test-Path -pathtype container

0

に、あなたは間違いなく検討すべきですそれ。

$ TestPath32 & $ TestPath64変数は、個々のコンピュータ値を割り当てる前に評価されます。私は単にnullになるので、両方とも$ falseになります。あなたは、単に一方で、私はあなたが配列を返すために、あなたのファイルの内容をフォーマットした願っています。また、あなたのために、ループ内

foreach ($computer in $computers) {  
    if (test-Connection -Cn $computer -quiet) { 
     $TestPath32 = Test-Path -path "\\$computer\$dest32b\*" 
     $TestPath64 = Test-Path -path "\\$computer\$dest64b\*" 
...... 
} 

それらを移動することができます。あなたは、単に「」でそれを区切ると ファイル内容読んでいない場合は、次の配列としてコンピュータ1、コンピュータ2、Computer3

をして、ファイルを読み込む直接

$computers = Get-Content 'c:\temp\computers.txt' # It will read it as an array only. YOu do not need any additional formatting in this case. 
+0

デフォルトでは 'Get-Content'はテキストファイルを配列として読み込みます。ファイル内の各行は配列内の項目を表します。各コンピュータがそれ自身の行である場合は、追加の書式設定は必要ありません。 –

0

GPOまたはスケジュールされたタスクを使用している間になりますより良い、または可能であればSCCMを使用して展開する場合は、です。あなたは右の基本的な考え方を得たが、foreachループに$TestPath32$TestPath64のあなたの割り当てを移動しました:

... 
foreach ($computer in $computers) { 
    if (test-Connection -Cn $computer -quiet) { 
     $TestPath32 = Test-Path -path "\\$computer\$dest32b\*" 
     $TestPath64 = Test-Path -path "\\$computer\$dest64b\*" 
     IF (!$TestPath32) { 
... 
+0

これは良いと思うけど、現時点では、テストパスが失敗し、PS全体が壊れてしまい、意図したように振る舞いません。 私は最後のELSEステートメントで失敗し続けたので、オンラインチェックをコメントアウトしました。何らかの理由で私が気づいていないので、1つ後にIF文を実行することは不可能です。 私はGOTOに頼る必要はないと確信しています。 メインの投稿に新しい結果を加えて編集します。 –

関連する問題