@@ -101,33 +101,33 @@ function Start-Process {
101
101
)
102
102
103
103
# Function to handle logging only if enabled
104
- function Log -Message {
104
+ function Write -Message {
105
105
param (
106
106
[string ]$Message
107
107
)
108
108
if ($env: LOG_DEBUG ) {
109
- $logFilePath = " $env: USERPROFILE \ProcessLog.txt "
109
+ $logFilePath = " $env: USERPROFILE \pwsh_powershell_debug.log "
110
110
$logEntry = " $ ( Get-Date ) - $Message "
111
111
Add-Content - Path $logFilePath - Value $logEntry
112
112
}
113
113
}
114
114
115
- Log - Message " Starting process: $FilePath with arguments: $ ( $ArgumentList -join ' ' ) "
115
+ Write -Message " Starting process: $FilePath with arguments: $ ( $ArgumentList -join ' ' ) "
116
116
117
117
try {
118
118
# Check if the file exists at the specified path
119
119
if (-not (Test-Path $FilePath - PathType Leaf)) {
120
- Log - Message " File not found: $FilePath "
120
+ Write -Message " File not found: $FilePath "
121
121
throw " File not found: $FilePath "
122
122
}
123
123
124
124
# Check if the file extension is .bat and rewrite the command if needed
125
125
if ([System.IO.Path ]::GetExtension($FilePath ).ToLower() -eq ' .bat' ) {
126
- Log - Message " Batch file detected: $FilePath "
126
+ Write -Message " Batch file detected: $FilePath "
127
127
128
128
$fileContent = Get-Content $FilePath - Raw
129
129
if ($fileContent -match ' \(echo %ERRORLEVEL%\) >' ) {
130
- Log - Message " Rewriting batch file: $FilePath "
130
+ Write -Message " Rewriting batch file: $FilePath "
131
131
$fileContent = $fileContent -replace ' \(echo %ERRORLEVEL%\) >' , ' echo %ERRORLEVEL% >'
132
132
Set-Content $FilePath - Value $fileContent
133
133
}
@@ -149,53 +149,53 @@ function Start-Process {
149
149
150
150
# Set working directory if provided
151
151
if ($WorkingDirectory ) {
152
- Log - Message " Setting working directory: $WorkingDirectory "
152
+ Write -Message " Setting working directory: $WorkingDirectory "
153
153
$processStartInfo.WorkingDirectory = $WorkingDirectory
154
154
}
155
155
156
156
# Set verb if provided
157
157
if ($Verb ) {
158
- Log - Message " Setting verb: $Verb "
158
+ Write -Message " Setting verb: $Verb "
159
159
$processStartInfo.Verb = $Verb
160
160
}
161
161
162
162
# Handle input/output redirection
163
163
if ($RedirectStandardOutput ) {
164
- Log - Message " Redirecting standard output to: $RedirectStandardOutput "
164
+ Write -Message " Redirecting standard output to: $RedirectStandardOutput "
165
165
$processStartInfo.RedirectStandardOutput = $true
166
166
$processStartInfo.StandardOutputFileName = $RedirectStandardOutput
167
167
}
168
168
if ($RedirectStandardError ) {
169
- Log - Message " Redirecting standard error to: $RedirectStandardError "
169
+ Write -Message " Redirecting standard error to: $RedirectStandardError "
170
170
$processStartInfo.RedirectStandardError = $true
171
171
$processStartInfo.StandardErrorFileName = $RedirectStandardError
172
172
}
173
173
if ($RedirectStandardInput ) {
174
- Log - Message " Redirecting standard input from: $RedirectStandardInput "
174
+ Write -Message " Redirecting standard input from: $RedirectStandardInput "
175
175
$processStartInfo.RedirectStandardInput = $true
176
176
}
177
177
178
178
# If credentials provided, load them
179
179
if ($Credential ) {
180
- Log - Message " Using provided credentials for: $ ( $Credential.UserName ) "
180
+ Write -Message " Using provided credentials for: $ ( $Credential.UserName ) "
181
181
$processStartInfo.UserName = $Credential.UserName
182
182
$password = $Credential.GetNetworkCredential ().Password
183
183
$processStartInfo.Password = (ConvertTo-SecureString $password - AsPlainText - Force)
184
184
}
185
185
186
186
# UseNewEnvironment flag (whether to inherit or create new environment variables)
187
187
if ($UseNewEnvironment ) {
188
- Log - Message " Clearing environment variables for new process"
188
+ Write -Message " Clearing environment variables for new process"
189
189
$processStartInfo.EnvironmentVariables.Clear ()
190
190
}
191
191
192
192
# Start process
193
- Log - Message " Starting process: $FilePath "
193
+ Write -Message " Starting process: $FilePath "
194
194
$process = [System.Diagnostics.Process ]::Start($processStartInfo )
195
195
196
196
# Log process start success
197
197
if ($process ) {
198
- Log - Message " Process started successfully: $FilePath (PID: $ ( $process.Id ) )"
198
+ Write -Message " Process started successfully: $FilePath (PID: $ ( $process.Id ) )"
199
199
}
200
200
201
201
# If PassThru is specified, return the process object
@@ -205,15 +205,15 @@ function Start-Process {
205
205
206
206
# Wait for the process to exit if the Wait switch is specified
207
207
if ($Wait ) {
208
- Log - Message " Waiting for process to exit: $FilePath "
208
+ Write -Message " Waiting for process to exit: $FilePath "
209
209
$process.WaitForExit ()
210
210
211
211
$exitCode = $process.ExitCode
212
- Log - Message " Process exited with code: $exitCode "
212
+ Write -Message " Process exited with code: $exitCode "
213
213
}
214
214
215
215
} catch {
216
- Log - Message " Error starting process: $_ "
216
+ Write -Message " Error starting process: $_ "
217
217
throw $_
218
218
}
219
219
}
0 commit comments