2016-05-19 7 views
1

コマンドプロンプトから実行するPowerShellスクリプトがありますが、ServerList.txtUrls.txtのファイルにはCannot find the path errorが表示されます。スクリプトは、スクリプトとファイルが存在するフォルダにディレクトリを変更すると動作します。powershellスクリプトの実行中にcmdでパスエラーが見つかりません

write-host "********* Changing IE Settings********************" 
$servers = Get-Content .\ServerList.txt 
$Urls = Get-Content .\Urls.txt 
$command ={ 
$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\InternetSettings\ZoneMap\Domains" 
Foreach ($url in $Urls) 
{ 
    $checkRegistryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\" + $url 
if(!(Test-Path $checkRegistryPath)) 
{ 
    write-host "Adding url to local intranet" 
    if($url -eq "localhost") 
    { 

    $key = (get-item HKCU:\).OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains", $true) 
    $subkey=$key.CreateSubKey('localhost') 
    $subkey.SetValue("http","1","DWORD") 
    $subkey.SetValue("https","1","DWORD") 
    $key.Close() 
    $subkey.Close() 


    } 
    elseif($url -like '*system*') 
    { 
    $key = (get-item HKCU:\).OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains", $true) 
    $subkey = $key.CreateSubKey('//system') 
    $subkey.SetValue("hcp","1","DWORD") 
    $key.Close() 
    $subkey.Close() 

    } 
    elseif($url -like '*next.loc*') 
    { 
     $key = (get-item HKCU:\).OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains", $true) 
     $key.CreateSubKey("next.loc") 
     $serverkey =(get-item HKCU:\).OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\next.loc", $true) 
     $servername= (([System.Uri]$url).Host).split('.') 
     $subkey=$serverkey.CreateSubKey($servername[0]) 
     $subkey.SetValue("http","1","DWORD") 
     $key.Close() 
     $serverkey.Close() 
     $subkey.close() 

    } 
} 
else 
{ 
    write-host $url "url already added to local intranet" 
} 
} 
} 

Foreach ($server in $servers) 
{ 
    if([string]::IsNullOrEmpty($server)) 
    { 
     Invoke-Command -ScriptBlock $command 
    } 
    else 
    { 
    Invoke-Command -Computer $server -ScriptBlock $command 
    } 

    } 
write-host "****** IE Settings Changed Sucessfully************" 

答えて

1

あなたは使用してスクリプトのパスを決定することができます:今、あなたはJoin-Pathコマンドレットを使用して、パスを結合するために$scriptPathを使用することができます

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition 

を:

$servers = Get-Content (Join-Path $scriptPath 'ServerList.txt') 
$Urls = Get-Content (Join-Path $scriptPath 'Urls.txt') 
+0

代わりに 'セット - を使用スクリプト内の作業ディレクトリを変更するにはLocation $ scriptPathを使用します。 –

+0

@AnsgarWiechersはい、これもうまくいきますが、スクリプトを実行するときに作業ディレクトリが変更されるのが好きではないので、可能な限り避けようとしていますか? –

+1

真実ですが、これを避ける/緩和するために 'Push-Location'と' Pop-Location'を使うことができます。 –

関連する問題