I have a service that reads from a database a string that is a powershell script. The code snippet from the c# service is:
psi = new ProcessStartInfo(psPath, "-executionpolicy bypass -file c:\temp\HeartBeatScript.ps1 -verbose");
psi.CreateNoWindow = false;
psi.UseShellExecute = true;
psi.Verb = "runas";
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(psi);
The script that is being run is as follows:
&{
Start-Transcript c:tempPSTranscript.txt
Set-PSDebug -Trace 2
$time = Get-Date
write-host "Starting" $time
$ServiceName = 'UsageMonitor'
$UpdateFromPath = '\FileShareTest SoftwareUserShare*'
$CopyTo = 'C:temp'
$arrService = Get-Service -Name $ServiceName
Start-Sleep -seconds 60
$time = Get-Date
write-host "Stopping service" $time
while ($arrService.Status -eq ''Running'')
{
Stop-Service $ServiceName
Start-Sleep -seconds 35
$arrService.Refresh()
}
Start-Sleep -seconds 10
$time = Get-Date
write-host "Copying Files" $time
Copy-Item -force -path $UpdateFromPath -destination $CopyTo
Start-Sleep -seconds 30
$time = Get-Date
write-host "Starting Service" $time
Start-Service $ServiceName
Start-Sleep -seconds 35
$arrService.Refresh()
while ($arrService.Status -ne 'Running')
{
Start-Service $ServiceName
Start-Sleep -seconds 35
$arrService.Refresh()
}
$time = Get-Date
write-host "complete" $time
} *>c:tempPSScriptOut.txt
The redirected output of the script (PSScriptOut.txt) is
Transcript started, output file is c:tempPSTranscript.txt
Starting 7/1/2024 12:24:00 PM
Stopping service 7/1/2024 12:25:01 PM
Copying Files 7/1/2024 12:25:46 PM
Starting Service 7/1/2024 12:26:16 PM
complete 7/1/2024 12:26:51 PM
The output generated to the transcript (PSTransscript.txt) is:
**********************
Windows PowerShell transcript start
Start time: 20240701122400
Username: domainSYSTEM
RunAs User: domainSYSTEM
Configuration Name:
Machine: LD318077 (Microsoft Windows NT 10.0.22631.0)
Host Application: C:WINDOWSSystem32WindowsPowerShellv1.0PowerShell.exe -executionpolicy bypass -file c:tempHeartBeatScript.ps1
Process ID: 60924
PSVersion: 5.1.22621.3672
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22621.3672
BuildVersion: 10.0.22621.3672
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Starting 7/1/2024 12:24:00 PM
Stopping service 7/1/2024 12:25:01 PM
Copying Files 7/1/2024 12:25:46 PM
Starting Service 7/1/2024 12:26:16 PM
complete 7/1/2024 12:26:51 PM
PS>$global:?
True
**********************
Windows PowerShell transcript end
End time: 20240701122651
**********************
I’ve left the debugging attempts in the code/scripts.
I see the service stopping. I see the service start.
What I don’t see are the files copied in the middle.
If I run the same script in the PSE environment, everything works. Service stops. Files are copied. Service starts.
I have a mechanism where I could supply alternate credentials for the PS script to run under. But, that would be inconvenient since that would involve another department.
Ideas on how I might get this to work?