简体   繁体   中英

How to Call Powershell Copy-Script from VBS Logon Script

I am currently in the process of converting a logon script from VBS to Powershell, and one of the subs, which updates a specific file if it is an older version, has been reduced to this:

sub UrgentSupportApp()
    On Error Resume Next

    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "powershell -file ""\\DC1\NETLOGON\PSSubs\UrgentSupportApp.ps1""", 0, true

    If Err <> 0 Then
        strMessage = "ERROR: Sub - UrgentSupportApp"
        DisplayCustomError(strMessage)
    End If
End Sub

But when I log in I am seeing this error:

Number (dec) : -2147352567

Number (hex) : &H80020009

Description : Unable to wait for process.

Source : WshShell.Run

I know the powershell works, as I have tested it as a standalone script. However, I am not sure if accessing it over the network has implications for the env:USERPROFILE variable (which user is in scope here?).

I tried changing the bWaitOnReturn parameter to false , and this resulted in no errors, but the script didn't actually do the work.

Does anyone have any tips as to how I can debug this or what might be going wrong?

The ps1 script looks like this:

$appsource = '\\file\Administration\Unused\Apps\IT Support App\IT Self Help.exe'
$apptarget = $env:USERPROFILE + '\desktop\IT Self Help.exe'

$oldapp1 = $env:USERPROFILE + '\desktop\Urgent IT Help Request.exe'
$oldapp2 = $env:USERPROFILE + '\desktop\IT Help Request.exe'

$currentversion = (Get-Command $appsource).FileVersionInfo.FileVersion

If (Test-Path $oldapp1) {Remove-Item $oldapp1}
If (Test-Path $oldapp2) {Remove-Item $oldapp2}

If (Test-Path $apptarget) {
    If (!((Get-Command $apptarget).FileVersionInfo.FileVersion -eq $currentversion)) {
        Copy-Item $appsource $env:USERPROFILE'\desktop' -Force
    }
}
Else {Copy-Item $appsource $env:USERPROFILE'\desktop' -Force}

It simply removes any of the files which have the old name, and then copies over the newest version if it doesn't already exist.

I was able to resolve this by changing the VBS call to:

objShell.Run("powershell.exe -noexit \\\\DC1\\NETLOGON\\PSSubs\\UrgentSupportApp.ps1"), 0

I'm not sure why this works, but it does.

Please note that this is being called on a thin client via a log on script when the user logs on to computer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM