Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading