I have a Windows batch script that copies an EXE or batchfile to a remote fileshare and runs it on the remote server using PowerShell WinRM. The line that runs the command remotely looks like this:
ECHO Invoke-Command -Authentication CredSSP -Credential %USERDOMAIN%\%USERNAME% -ComputerName %SERVER_NAME% -ScriptBlock { cd (gwmi Win32_Share -Filter "name='%SHARE_NAME%'").Path ; .\%MY_EXE_TO_RUN% } |PowerShell.exe -Command -
I need to change the current directory to the fileshare directory where the EXE/batch file to be run is located (it relies on this). It also must be a local directory - if I try to CD \\%SERVER_NAME%\%SHARE_NAME%
this works for an EXE, but fails for a batch file, cmd.exe doesn't support a UNC path as current directory. However, I also don't want to hardcode in the script the local path for the share, so I try to look it up in WMI.
This works fine for admins, but fails for non-admins with an Access denied error. These non-admin users can RDP to the server, open PowerShell and successfully run gwmi Win32_Share
there. So it seems like the permissions checks are somehow different when running via WinRM!
I found http://www.adilhindistan.com/2013/10/permissions-to-access-wmi-remotely.html but it seems pretty complicated to do on many servers. I don't need remote WMI access for anything other than this. Perhaps there is another way to resolve the local path for the share? Or, alternatively, can I somehow run the WMI query and have the local permission checks applied to it? After all, I am already code on the remote server, so there should be some way to do this.