Home > OS >  can someone please check the code it does not work
can someone please check the code it does not work

Time:11-05

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}  
}

CodePudding user response:

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}  
}

CodePudding user response:

Here are a few more recommendations for your script:

  1. New-PSSession $computer creates a connection to another computer, but doesn't cause the following commands to run over there. You would want to use something like Invoke-Command or Enter-PSSession like you do later in your script.

  2. The files and folders you need can be copied remotely using the default Admin$ share on windows, which links to the system directory:

# Create folders
New-Item -ItemType directory -Path "\\$Computer\Admin$\Temp\FileTransfer" 
New-Item -ItemType directory -Path "\\$Computer\Admin$\Temp\DriverInstallation"
New-Item -ItemType directory -Path "\\$Computer\Admin$\Temp\SoftwareInstallation" 

# Copy installation files
Copy-Item $openFileDialog1 "\\$Computer\Admin$\Temp\FileTransfer" -Recurse
Copy-Item $openFileDialog2 "\\$Computer\Admin$\Temp\DriverTransfer" -Recurse
Copy-Item $OpenFileDialog3 "\\$Computer\Admin$\Temp\SoftwareInstallation" -Recurse
  1. If you want to use your local variables like $openFileDialog1 When using Invoke-Command, you must specify the Using: scope to tell powershell you want the local ones and not something from the remote machine:
Write-Host "Installing Software and Drivers on $Computer"

Invoke-Command -ComputerName $Computer -ScriptBlock {
  Start-Process  "$env:SystemRoot\Temp\DriverTransfer\$Using:openFileDialog2" -ArgumentList "/q" -Wait
  Start-Process  "$env:SystemRoot\Temp\SoftwareInstallation\$Using:OpenFileDialog3" -ArgumentList "/q" -Wait
}
  • Related