can someone please check the code it does not work

I would like to install the selected programs and drivers on a newly created folder on a remote computer. unfortunately it does not work properly… Does anyone know what this could be or has a tip for me I am relatively new to Powershell. Thanks for your answers

$handler_submitwindow2_Click= 
{
$Computer = Read-Host -Prompt 'Enter the Computer Name you are accessing'
New-PSSession -ComputerName $Computer
#----------Install Software On PC----------#
New-Item -ItemType directory -Path ".\%systemroot%\Temp\FileTransfer" 
New-Item -ItemType directory -Path ".\%systemroot%\Temp\DriverInstallation"
New-Item -ItemType directory -Path ".\%systemroot%\Temp\SoftwareInstallation" 

    Copy-Item $openFileDialog1 ".\%systemroot%\Temp\FileTransfer" -Recurse
    Copy-Item $openFileDialog2 ".\%systemroot%\Temp\DriverTransfer" -Recurse
    Copy-Item $OpenFileDialog3 ".\%systemroot%\Temp\SoftwareInstallation" -Recurse

    Write-Host "Software and Drivers get installed on $Computer"

    Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process  $openFileDialog2 -ArgumentList "/q" -Wait}  
    Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process  $OpenFileDialog3 -ArgumentList "/q" -Wait}  
}

>Solution :

Two mistakes here.

You are using the .\ operator, which represents the current directory. You then attempt to use the environment variable %systemroot%, which would resolve to something like:

C:\Your\Current\Directory\C:\Windows\Temp\FileTransfer

Which is wrong. You should remove that operator if you’re not using a relative path from your current directory.

In addition, as @Theo mentioned in the comments, %systemroot% does not work in PowerShell. You must instead use the syntax $env:SystemRoot.

Therefore, your final script would look like this:

$handler_submitwindow2_Click= 
{
    $Computer = Read-Host -Prompt 'Enter the Computer Name you are accessing'
    New-PSSession -ComputerName $Computer
    #----------Install Software On PC----------#
    New-Item -ItemType directory -Path "$env:SystemRoot\Temp\FileTransfer" 
    New-Item -ItemType directory -Path "$env:SystemRoot\Temp\DriverInstallation"
    New-Item -ItemType directory -Path "$env:SystemRoot\Temp\SoftwareInstallation" 

    Copy-Item $openFileDialog1 "$env:SystemRoot\Temp\FileTransfer" -Recurse
    Copy-Item $openFileDialog2 "$env:SystemRoot\Temp\DriverTransfer" -Recurse
    Copy-Item $OpenFileDialog3 "$env:SystemRoot\Temp\SoftwareInstallation" -Recurse

    Write-Host "Software and Drivers get installed on $Computer"

    Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process  $openFileDialog2 -ArgumentList "/q" -Wait}  
    Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process  $OpenFileDialog3 -ArgumentList "/q" -Wait}  
}

Leave a Reply