Powershell Error

This topic contains 4 replies, has 3 voices, and was last updated by  cosmic 3 months ago.

  • Author
    Posts
  • #79397

    cosmic
    Participant

    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

    Will Anderson
    Keymaster

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

  • #79402

    Don Jones
    Keymaster

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

    C`$

    Should help.

  • #79490

    cosmic
    Participant
    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

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

You must be logged in to reply to this topic.