2017-07-21 14 views
1

私を助けてください。 、Powershellでは、do-whileループを使用してリモートコンピュータにIPアドレスを使用してディレクトリを作成する方法

  1. を「フォルダ名」は存在しない場合は、フォルダ名(フォルダ名)をユーザーに尋ね、IPアドレス(targetipaddress)
  2. をターゲット:私がdo-whileループ、次のように使用してPowerShellスクリプトを作成しようとしようとしています"C:\"ドライブに "foldername"を作成
  3. "targetipaddress"をターゲットパスの一部として使用して、ネットワークドライブパス "S:\"のサブフォルダから内容をコピーし、 "foldername" 「C:\」パスに入力します。
  4. ユーザーが「quit」と入力すると、スクリプトは終了します。

以下の私のコード:

$var = "welcome to IP address and folder creation world" 
echo $var 

# create foldername until user type "quit" 
$output = (command) 
do { 

$foldername = Read-Host "enter folder name " 
$targetipaddress = Read-Host "enter target ip address " 

# create folder name only if it does not exist 
function createFolder { 
$path = "\\$targetipaddress\C$\$foldername " 
If (!(Test-Path $path)) 
{ 
     New-Item -ItemType Directory -Force $path 
} 

} # end of function createFolder 

# copy contents from a sub folder of a network drive path and 
# and paste them onto "C:\" drive path using "targetipaddress" and "foldername" 

function copyContents { 
Copy-Item S:\sourcefolder\subfolder\*.* \\$targetipaddress\C$\$foldername\ -Recurse 
} 

} while ($foldername = "quit") # end of do-while loop 
+4

あなたの質問をすることを忘れないでください。 –

+1

関数**をdo..while **内で定義しますが、決して**使用しないでください** **? – LotPings

+0

こんにちはLotPings、do-whileループが正しいアプローチであると私は混乱します。たぶん "do loop until"です。ありがとう –

答えて

1

の作業例については以下を参照してください、慎重に読んで、自分自身で、このいずれかを比較します。

いくつかの問題/変更。

  • echo?書き込みホストである必要があります。
  • 不要で未使用の機能です。
  • $ output =(command)、何もしません。
  • do while、私はwhile($ true)を使い、そこから抜け出すのが好きです。
  • オプション、エラー処理。
  • powershellはequelsロジックでは=(私はあなたが==を意味していたとは思っていませんが)を使用しませんが、-eq
  • その他のいくつかの問題や変更を加えました。

いくつかの読み物:

  • 比較演算子を参照してくださいリンクをマイクシェリル '猫のリコール'
  • Continue
  • Break

から、あなたは、このコードでエラーが発生した場合してください$ source、$ foldername、$ targetipaddress、$ pathの入力を投稿してみます。

コード:

clear-host #clears your console, optional. 
$ErrorActionPreference = "Stop" #prevents al lot of posible disasters from happening. 

Write-Host "welcome to IP address and folder creation world" #Powershell way of writing to the console. 

$source = "S:\sourcefolder\subfolder\*" 
#$source = "C:\source\*" i used this for testing on my local machine. 

# create foldername until user type "quit" 
While ($true){ 

    " 

    "#Add some white space between the questions. 

    $foldername = Read-Host "enter folder name " 
    If ($foldername -eq "quit") {break} #exit the while loop 

    $targetipaddress = Read-Host "enter target ip address " #you could use a hostname to. 

    $path = "\\$targetipaddress\C$\$foldername" 

    #if connection to targetipaddress fails then abort. 
    if(!(Test-Connection $targetipaddress -ErrorAction SilentlyContinue)) #look at the ! it means it should be false. 
    { 
     Write-Warning "Could not connect to '$targetipaddress'" 
     continue #goes back to the begining of the while loop. 
    } 

    #if path does not exist try to create it. 
    if(!(Test-Path $path)) #look at the ! it means it should be false. 
    { 
     try { New-Item -ItemType Directory -Path (Split-Path -Parent $path) -Name $foldername | Out-null } #Out-null prevenst unwanted console output. 
     catch { 
      Write-Warning "Could not create folder '$foldername' on '$targetipaddress'" 
      continue #goes back to the begining of the while loop. 
     } 
    } 

    #try to copy files. 
    try { Copy-Item -Path $source -Destination $path -Recurse -Force } 
    catch { 
     Write-Warning "An Error occured while copying the files from '$source' to '$path'" 
     continue #goes back to the begining of the while loop. 
    } 

    #if all goes well then tell them ;) 
    Write-Host "files copied successfully from $source to $path" 

} 
+0

あなたは素晴らしい、SteloNLDです。ありがとうございました。私はすぐにこれを試しています。おかげで再び –

+0

SteloNLD、私のコンピュータでローカルに実行し、素晴らしい作品。ありがとうございました。 到達可能でないIPをユーザーが入力すると、ループが戻り、フォルダー名の入力を求められます。 質問:いつ次のことを守っていますか? #if targetipaddressへの接続が失敗した場合は中止します。 "? if(!(Test-Connection $ targetipaddress -ErrorAction SilentlyContinue)) #これは偽であることを意味します –

+0

はい、'if接続後の部分to targetipaddressに失敗した後、中止してループをリセットすると、 '$ targetipaddress'に接続できませんでした。 – SteloNLD

関連する問題