Batch file calls powershell script RRS feed

  • Question

  • I have a batch file that is calling a powershell script.  When I am logged in as an administrator onto the machine and I run the batch file it runs properly and the powershell script is executed.

    If I log in as myself (who has administrator privileges) onto that same machine and run the batch file I get the following error: "Set-ItemProperty: Requested registry access is not allowed."

    If I literally just right click on the command prompt and say Run as Administrator and then run the batch file it runs properly.  Is there a way I can specify administrator credentials when launching the powershell file?

    I am using Kace to call the batch file and I tell it to run as administrator.  I think the problem that I'm running into is when the batch file calls the powershell file that portion isn't running as administrator so that's why it's failing it seems like.

    Thank you.

    Friday, August 1, 2014 6:24 PM

All replies

  • Hello,

    how are you running the batch file? because it could also be that you are running the -bat file as System account and the powershell needs an actual account to work.

    Monday, August 4, 2014 12:08 PM
  • When I call the batch file through Kace it is being run as a domain administrator and then it executes the powershell script so I don't believe the powershell script is getting run with elevated privileges.  Here's how it's setup:

    Syntax of batch file:

    powershell.exe -ExecutionPolicy Bypass -Command "C:\ProgramData\Dell\KACE\kbots_cache\packages\kbots\357\Name.ps1"

    Syntax of powershell script:

    $computerName = $env:COMPUTERNAME.ToUpper()
    $keyPath = 'HKLM:\System\CurrentControlSet\Services\TCPIP\Parameters'
    $keyOne = 'Hostname'
    $keyTwo = 'NV Hostname'

    Set-ItemProperty -Path $keyPath -Name $keyOne -Value $computerName
    Set-ItemProperty -Path $keyPath -Name $keyTwo -Value $computerName

    Is there somewhere within the powershell file that I can specify administrator credentials?  Is that what will be needed?  Thank you.

    Monday, August 4, 2014 12:33 PM
  • Hello, Sorry I didn't understand the question. First thing I have to ask is why don't you just run the ps-file and not the cmd-file? How ever if Kace wont support this and you have to run the cmd file have you try to start a powershell script that would start the actual powershell script "as admin". I dont think there is any valid way of starting the powershell script "as admin" from the cmd-prompt. You need to start the batch that will start the first powershell file that will start the actual powershell file containing the code. Does this make sense to you? Start-Process powershell -verb runas -ArgumentList "-file fullpathofthescript" this is the only line on your first ps1-file.
    Monday, August 4, 2014 7:25 PM