Maybe can anyone help with my question?
I am trying to schedule an Powershell script and run it as "system".
It looks like it had not sufficient rights:
- it can not find an executable (and run it)
- when it checks the installation of a Window feature it does not detect the feature that is already installed: Get-WindowsFeature -Name FS-Resource-Manager
This is the Powershell script:
---
Function LogWrite{ Param ([string]$logstring) Add-content $Logfile -value $logstring}################################ Functions ################################$majorVer = [System.Environment]::OSVersion.Version.Major$minorVer = [System.Environment]::OSVersion.Version.Minor$Logfile = "C:\temp\Test-log.txt"$theDate = Get-Dateif (Test-Path $Logfile) { Remove-Item $Logfile}$username = [Environment]::UserNameWrite-Host "Running test ..."LogWrite "Testing... ($theDate)"LogWrite "Detected OS version numbers: major = $majorVer, minor = $minorVer"LogWrite "Running in user environment of $username"$exe = "c:\windows\system32\filescrn.exe"if (Test-Path $exe){ LogWrite "Filescrn EXE found: OK"}else{ LogWrite "Error - Filescrn EXE NOT found"}$pol = Get-ExecutionPolicyLogWrite "Execution policy: $pol"if ($majorVer -ge 6){ $checkFSRM = Get-WindowsFeature -Name FS-Resource-Manager LogWrite "FSRM check results: $checkFSRM"}LogWrite "Finished"
When I run the agent procedure by agent procedure this is the content in the log file:
Testing... (11/04/2016 16:13:20)Detected OS version numbers: major = 6, minor = 1Running in user environment of SYSTEMError - Filescrn EXE NOT foundExecution policy: UnrestrictedFSRM check results: Finished
When I run this manually in elevated PS command line the results are:
Testing... (11/04/2016 16:16:54)Detected OS version numbers: major = 6, minor = 1Running in user environment of adm-mwenkeFilescrn EXE found: OKExecution policy: UnrestrictedFSRM check results: Microsoft.Windows.ServerManager.Commands.FeatureFinished
Has anyone suggestions?
I tried these statements in the agent procedure:
1)
<Statement name="Execute Powershell Command (32-bit, Run As System)" continueOnFail="false" osType="Windows"><Parameter xsi:type="StringParameter" name="Parameter1" value="#workdir#\FSRM\tools\Test.ps1"/><Parameter xsi:type="StringParameter" name="Parameter2" value=""/><Parameter xsi:type="StringParameter" name="Parameter3" value="True"/></Statement>
2)
<Statement name="ExecuteShellCommand" continueOnFail="true" osType="None"><Parameter xsi:type="StringParameter" name="Command" value="powershell.exe -nologo -executionpolicy bypass -noprofile -file "#workdir#\FSRM\tools\Test.ps1""/><Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System"/><Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False"/></Statement>
3)
<Statement name="ExecuteShellCommand" continueOnFail="false" osType="None"><Parameter xsi:type="StringParameter" name="Command" value=""C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Unrestricted -NoLogo -Command "#workdir#\FSRM\tools\DeployCryptoBlocker.ps1""/><Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System"/><Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False"/></Statement>
How about running it with Start-process -Verb RunAs?
More info here:
ss64.com/.../syntax-elevate.html
I presume it's a 64-bit OS so try running the 64-bit versions of your commands to match the native OS.
Thank yo for the comments.
@neuvoja- I want a solution that works at several netwerkt (independant of a network account)
@Combo: o do not understand what you mean
I have added a UseCredential command to the procedure so the Powershell script is executed under the agent credentials:
Testing... (11/07/2016 12:31:01)
Detected OS version numbers: major = 6, minor = 1
Running in user environment of sa_kaseya
Error - Filescrn EXE NOT found
Execution policy: Unrestricted
FSRM check results:
Finished
When I log to the server using the sa_kaseya user, and run the script in a elevated PS shell the results in the log file are:
Testing... (11/07/2016 11:53:28)
Filescrn EXE found: OK
FSRM check results: Microsoft.Windows.ServerManager.Commands.Feature
Procedure:
So the Powershell script has still not enough privileges... Anyone?