1

I have the below PowerShell script which I want to run from command prompt but it gives me Cannot find the path error for files ServerList.txt and Urls.txt. Script works when I change directory to the folder where the script and files exists.

 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 Answer 1

1

You can determine the path of your script using:

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

Now you can use the $scriptPath to combine your path using the Join-Path cmdlet:

$servers = Get-Content (Join-Path $scriptPath  'ServerList.txt')
$Urls = Get-Content (Join-Path $scriptPath 'Urls.txt')
Sign up to request clarification or add additional context in comments.

3 Comments

Alternatively use Set-Location $scriptPath to change the working directory in the script.
@AnsgarWiechers Yes, this works too but I try to avoid it whenever possible because I don't like my working directory to get changed when I run a script, don't you?
True, although you could use Push-Location and Pop-Location to avoid/mitigate this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.