Hi all, I have a script that does a couple of things, it’s primary function is to install software remotely and quietly
- Prompts user to input machine name
- Set-Location to network shared directory containing software
- From that directory, Copy-Item to remote admin share
- Invoke-Command msiexec.exe to quietly install software using local path to copied software
- Remove-Item the copied software
Running the script, the file get successfully copied to their location. However when trying to Invoke-Command, the software does not install. On top of that, when trying to remove the file, error feeds back:
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemC
ommand
I recognize that there’s something wrong with how I’ve stored the path in the variable, but I’ve had the script write the output of the install directory path and it looks fine. Also, if I do these step by step in the terminal, software successfully installs quietly. Here is my actual script:
$workingDirectory = $PSScriptRoot
$user = (whoami).Split('\')[1]
$remoteComputer = Read-Host "Please enter computer name"
$t2Profile = "\\computer\C$\Users\$user\installdirectory" # this uses admin share to copy files over
if (Test-Path $t2Profile){
if(-not(Test-Path "$t2Profile\installfolder")){
New-Item -ItemType Directory "$t2Profile\installfolder"
} else {
$atlas = "\\path\to\file\share\Mozilla Firefox\126.0" # file in \126.0 directory is called "Firefox Setup 126.0.msi"
$msiName = (Get-ChildItem $atlas).Name
Set-Location $atlas # set location to the network shared directory so that Copy-Item circumvents double-hop issues
Write-Output "Copying $atlas\$msiName to $remoteComputer"
Copy-Item -Path ".\*msi" -Destination "$t2Profile\installfolder" # Copy the .msi setup file to the \installfolder
Set-Location $workingDirectory
$remoteDirectory = "\\$remoteComputer\C$\Users\$user\installdirectory\$msiName"
$installDirectory = "C:\Users\$user\installdirectory\$msiName" # this is the variable I use for the path in invoke-command
if(Test-Path "$remoteDirectory"){
Invoke-Command -ComputerName $remoteComputer -ScriptBlock {msiexec.exe /i "$installDirectory" /qn} #invoke msiexec on the remote machine using path to copied .msi
Invoke-Command -ComputerName $remoteComputer -ScriptBlock {Remove-Item "$installDirectory"}
} else {
Write-Host "Something went wrong"
}
}
} else {
Write-Host "Profile has not been created on this machine. Please use 'Enter-PSSession <computer name>' to add your profile, then rerun this script."
}
If anyone has recommendations or ideas please let me know. Thank you!