Powershell Error

Welcome Forums General PowerShell Q&A Powershell Error

Viewing 4 reply threads
  • Author
    Posts
    • #79397
      Participant
      Topics: 5
      Replies: 26
      Points: 0
      Rank: Member

      What is the problem?

      Test-Path : Illegal characters in path.
      At C:\Users\baa065sa\Powershell skript\best.ps1:67 char:16
      +            If (Test-Path $Destination)
      +                ~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (\\servertest.l...L_8.1.1_09_005P:String) [Test-Path
         on
          + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand

      THIS IS MY SCRIPT

        $RootDestinationPath = "\\$Server\C$\Cosmicinst\"
      
      
              $Destination = "$RootDestinationPath\$Folder1\$Folder2"
      
      
                 If (Test-Path $Destination)
                   {
                    Write-Host "Katalogen finns redan i IX3 $server ." -ForegroundColor yellow
               }
          
           
      
    • #79400
      Participant
      Topics: 2
      Replies: 234
      Points: 189
      Rank: Participant

      Try $Destination = ($RootDestinationPath + '\' + $Folder1+ '\' +$Folder2)

    • #79402
      Keymaster
      Topics: 13
      Replies: 4872
      Points: 1,844
      Helping HandTeam Member
      Rank: Community Hero

      Using $ inside double quotes triggers a feature – your C$ is confusing it.

      C`$

      Should help.

    • #79490
      Participant
      Topics: 5
      Replies: 26
      Points: 0
      Rank: Member
      Test-Path : Illegal characters in path.
      At C:\Users\baa065sa\Powershell skript\sugest.ps1:28 char:9
      +     If (Test-Path $Destination)
      +         ~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (\\    \C$\Cosmicin...L_8.1.1_09_006P:String) [Test-Path], ArgumentExcep
      ti    on
          + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand
      
      Test-Path : Illegal characters in path.
      At C:\Users\baa065sa\Powershell skript\sugest.ps1:42 char:9
      +         If (!(Test-Path "$RootDestinationPath\$Folder1"))
      +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (\\    \C$\Cosmicinst\LUL_8.1.1_09:String) [Test-Path], ArgumentExceptio
      n
          + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand
      
      New-Item : Illegal characters in path.
      At C:\Users\baa065sa\Powershell skript\sugest.ps1:48 char:11
      +                 New-Item -Name $Folder1 -Path $RootDestinationPath -ItemType Directory
      +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (\\    \C$\Cosmicinst\LUL_8.1.1_09:String) [New-Item], ArgumentException
          + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.NewItemCommand
      
      New-Item : Illegal characters in path.
      At C:\Users\baa065sa\Powershell skript\sugest.ps1:55 char:13
      +             New-Item -Name $Folder2 -Path "$RootDestinationPath\$Folder1" -ItemT ...
      
      
      
    • #79493
      Participant
      Topics: 5
      Replies: 26
      Points: 0
      Rank: Member
      $RootSourcePath = "D:\CosmicInst\R8"
      
      $Folder1 = Read-Host "Enter folder name under $RootSourcePath that you want to look for."
      
      $Folder2 = Read-Host "Enter the sub-folder name under $RootSourcePath\$Folder1 that you want to look for."
      
      $Source = "$RootSourcePath\$Folder1\$Folder2\Products\Manualhandling\IX3" 
      
      
      
      If(Test-Path $Source)
      
      {
        
      	$ServerList = Get-Content 'C:\Users\baa065sa\Powershell skript\Levtestservrar.txt'
      
      
              ForEach ($Server in $ServerList)
        
        {
          
      	  $RootDestinationPath = "\\$Server\C$\Cosmicinst"
      
      	  $Destination = "$RootDestinationPath\$Folder1\$Folder2"
      
      
      
          If (Test-Path $Destination)
          
      	
      	{
            
      	  Write-Host "Katalogen finns redan i IX3 LEVTEST." -ForegroundColor yellow
            
      
              }
          
      	Else
          
      	{
            
      		If (!(Test-Path "$RootDestinationPath\$Folder1"))
            
      		{
              
      			#If the parent folder of $Folder2 (i.e. $Folder1) doesn't exist under $RootDestinationPath, create it.
      
              		New-Item -Name $Folder1 -Path $RootDestinationPath -ItemType Directory
      	
              }
            
      	 
      	    #Since the test of the $Destination path failed, we know the $Folder2 folder also doesn't exist, so create it under $RootDestinationPath\$Folder1.
      
                  New-Item -Name $Folder2 -Path "$RootDestinationPath\$Folder1" -ItemType Directory
          
      	  }
      
      
      
      
          Copy-Item -Path $Source -Destination $Destination -Recurse
        
        }
      
      }
Viewing 4 reply threads
  • The topic ‘Powershell Error’ is closed to new replies.