Powershell Action Step throws "access denied" error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 06:49 AM
When I expose my credentials in script it's working fine but when i try different methods it's gives result with error
Output:
PowerShell Step:
script:
$username = "a"## if i pass username password here it is working fine
$password = ConvertTo-SecureString "a" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
##$userName = ($cred).username
##$decryptedPassword = (New-Object PSCredential 0, $cred.Password).GetNetworkCredential().Password
##$encryptedPassword = ConvertTo-SecureString -String $decryptedPassword -AsPlainText -Force
##$customCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $encryptedPassword
### Get current timestamp
Function getDT() {
(Get-Date -Format "yyyy-MM-dd HH:mm:ss ")
}
### Checks if service exists
Function existService($ServiceName) {
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Get-Service -Name $Args[0] -ErrorAction SilentlyContinue }
}
### Gets the service
Function getService($ServiceName) {
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Get-Service -Name $Args[0] }
}
### Validates if service exists and provides service status
Function getServiceStatus ($ServiceName) {
If ((existService $ServiceName)) {
$ServiceStatus = (getService $ServiceName).Status
Write-Host (getDT) "Service '$ServiceName' status = '$ServiceStatus'"
}
Else {
Write-Host (getDT) "Service '$ServiceName' not found"
}
}
### Returns 'true' when service is running
Function isServiceRunning($ServiceName) {
(getService $ServiceName).Status -eq 'Running'
}
### Returns 'true' when service is stopped
Function isServiceStopped($ServiceName) {
(getService $ServiceName).Status -eq 'Stopped'
}
Write-Host (getDT) "Initiating '$ServiceAction' of the service '$ServiceName' hosted by '$ComputerName'"
### Create PS session
$Session = New-PSSession -ComputerName Servicenow-poc -credential $cred
Write-Host (getDT) "Created PS session '$Session'"
### Validates if service name exists
If (existService $ServiceName) {
## 'Stop' condition
If ($ServiceAction -eq 'Stop') {
If ((isServiceRunning $ServiceName)) {
Write-Host (getDT) "Service '$ServiceName' is running, preparing to stop..."
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Stop-Service -Name $Args[0] -ErrorAction SilentlyContinue }
getServiceStatus $ServiceName
}
ElseIf ((isServiceStopped $ServiceName)) {
Write-Host (getDT) "Service '$ServiceName' already stopped!"
}
Else {
Write-Host (getDT) "Service '$ServiceName' status = '$ServiceStatus'"
}
}
## 'Start' condition
ElseIf ($ServiceAction -eq 'Start') {
If ((isServiceRunning $ServiceName)) {
Write-Host (getDT) $ServiceName "already running!"
}
ElseIf ((isServiceStopped $ServiceName)) {
Write-Host (getDT) "Service '$ServiceName' is stopped, preparing to start..."
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Start-Service -Name $Args[0] -ErrorAction SilentlyContinue }
getServiceStatus $ServiceName
}
Else {
Write-Host (getDT) "Service '$ServiceName' status = '$ServiceStatus'"
}
}
## 'Restart' condition
ElseIf ($ServiceAction -eq 'Restart') {
If ((isServiceRunning $ServiceName)) {
Write-Host (getDt) "Service '$ServiceName' is running, preparing to restart..."
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Stop-Service -Name $Args[0] -ErrorAction SilentlyContinue }
getServiceStatus $ServiceName
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Start-Service -Name $Args[0] -ErrorAction SilentlyContinue }
getServiceStatus $ServiceName
}
ElseIf ((isServiceStopped $ServiceName)) {
Write-Host (getDT) "Service '$ServiceName' is stopped, preparing to start..."
Invoke-Command -Session $Session -ArgumentList $ServiceName -ScriptBlock { Start-Service -Name $Args[0] -ErrorAction SilentlyContinue }
getServiceStatus $ServiceName
}
}
## Condition if action is anything other than 'Stop', 'Start', 'Restart'
Else {
Write-Host (getDT) "Service action parameter is missing or invalid!"
}
}
### Condition if provided service Name is invalid
Else {
Write-Host (getDT) "Service '$ServiceName' not found"
}
Write-Host "Closing PS session '$Session'"
Remove-PSSession $Session
even if i pass a as username password it's giving result and error but if i try to fetch it's giving access denied error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 02:22 PM
almost a year later and no replies - thanks community - we really need this one and I'm finding the same thing. @Sahil Khan1 did you ever figure out what the issue was and why it's not taking the credentials unless you hard code them?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 02:27 PM
Hello Jeffrey,
yes, I remember only due to some security constrain I am unable to get credentials in script