Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PS Remoting is disabled. #21

Closed
WinOpsEngineer opened this issue Feb 10, 2019 · 7 comments
Closed

PS Remoting is disabled. #21

WinOpsEngineer opened this issue Feb 10, 2019 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@WinOpsEngineer
Copy link

WinOpsEngineer commented Feb 10, 2019

I am unable to use invoke-comandas because powershell remoting is prohibited from being enabled.

I have been executing powershell scriptblocks remotely using wmi and would love to see if this functionality be added if possible.

here is what I have been using:

function Invoke-ScheduledScriptBlock {
[CmdletBinding()]
param
(
[Parameter()]
[string[]]$ComputerName ,

    [ValidateNotNull()]
    [System.Management.Automation.PSCredential]
    [System.Management.Automation.Credential()]
    $Credential = [System.Management.Automation.PSCredential]::Empty  ,
    [ScriptBlock]$ScriptBlock
)

$username = $Credential.Username
$password = $Credential.GetNetworkCredential().Password
$command = $scriptblock.ToString()
$command = $command + ' ; start-sleep -Seconds 2 ; SCHTASKS /Delete /TN OnDemand /F'
if($command.Length -lt '200'){
$script = @"
`$powerShellCommand =
"powershell.exe -noprofile -executionpolicy Unrestricted -command $command "

schtasks /CREATE /TN 'OnDemand' /SC WEEKLY /RL HIGHEST /RU $username /RP $password
/TR "`$powerShellCommand" /F

schtasks /RUN /TN 'OnDemand' | Out-String
"@
} Else {
$ps1 = ([system.guid]::newguid().tostring().split('-')[0]) + '.ps1'
$command | Out-File "\$ComputerName\c$\Windows\Temp$ps1" -Force

$script = @"
`$powerShellCommand =
"powershell.exe -noprofile -executionpolicy Unrestricted -file c:\windows\temp$ps1 "

schtasks /CREATE /TN 'OnDemand' /SC WEEKLY /RL HIGHEST /RU $username /RP $password
/TR "`$powerShellCommand" /F

schtasks /RUN /TN 'OnDemand' | Out-String
"@

}

$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($script)
$encoded = [Convert]::ToBase64String($commandBytes)
$command = "powershell -NoProfile -EncodedCommand $encoded"
$null = Invoke-WmiMethod -Computer $computername -Credential $AdminCred `
Win32_Process Create -Args $command

}

@WinOpsEngineer
Copy link
Author

$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($ScriptBlock)
$encoded = [Convert]::ToBase64String($commandBytes)
$command = "powershell -NoProfile -EncodedCommand $encoded"
$null = Invoke-WmiMethod -Computer $computername -Credential $Credential Win32_Process Create -Args $command

@WinOpsEngineer
Copy link
Author

above is the basic wmi remote method of executing powershell remotely

@mkellerman
Copy link
Owner

Invoke-CommandAs -AsWmiMethod ?

This might need to be implement as a whole separate function. As I’d want to replicate all the Invoke-WmiMethod parameters, simply adding -AsSystem, -AsCredential and -AsGSMA

Invoke-WmiMethodAs ?

@mkellerman mkellerman added enhancement New feature or request good first issue Good for newcomers labels Feb 11, 2019
@mkellerman
Copy link
Owner

After playing around with the code, I think this might need to be it's own module.

the expectation, is to convert the Invoke-Command, into a Invoke-Command[As], adding a few custom parameters to the regular Invoke-Command function.

Will need to investigate more how this could be implemented. Please take a look at the private function Invoke-ScheduledTask, to help you accelerate some of your functionality.

I'd be open to start a new project, and get the structure going on if you'd want to help out?

For now, I'll close the issue, please DM me on twitter.. ;)

@mkellerman
Copy link
Owner

https://github.com/mkellerman/Invoke-CommandAs/tree/mk-feature-wmicommand

image

Let me know if this works for you! Obviously, there is no output... :(

@mkellerman mkellerman reopened this Feb 21, 2019
@mkellerman
Copy link
Owner

Quick and dirty way to see if it works:

$ComputerName = 'W2012R2'
$Credential = Get-Credential 'Administrator'

$ScriptBlock = {

    New-Item -Path C:\Temp -ItemType Directory -Force | Out-Null
    [System.Security.Principal.Windowsidentity]::GetCurrent() | Out-File "C:\Temp\pwsh_output.txt"

}

$WmiProcess = Invoke-WmiCommandAs -ScriptBlock $ScriptBlock -ComputerName $ComputerName -Credential $Credential -AsSystem

While (Get-WmiObject -Class 'Win32_Process' -Filter "ProcessId='$($WmiProcess.ProcessId)'" -ComputerName $ComputerName -Credential $Credential -EA 0) {
    Write-Warning "$(Get-Date): Waiting..."
    Start-Sleep -Milliseconds 200
}

Then go check the content of the file at: C:\Temp\pwsh_output.txt

@mkellerman
Copy link
Owner

Closed. Please re-open ticket if you have any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants