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

Add CMScript deployement #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions PowerSCCM.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5930,3 +5930,92 @@ function Revoke-WmiNameSpaceRead {
throw "SetSecurityDescriptor failed: $($Output.ReturnValue)"
}
}


function New-CMScriptDeployement {
<#
.SYNOPSIS

Permits to deploy a PowerShell script (called a CMScript) on a distant machine with SCCM, instead of an application.
Use the script 'configurationmanager.psd1' by Microsoft, normally presents on a SCCM server.

.PARAMETER CMDrive

Configuration Manager site drive to use, will be created if it doesn't exist.

.PARAMETER ServerFQDN

Site server FQDN for the drive root (the SCCM server basically).

.PARAMETER TargetDevice

The target computer to deploy the script.

.PARAMETER Path

The local path of the PowerShell script to execute.

.PARAMETER ScriptName

The name the CMScript will have.

.PARAMETER ManagerPath

The local path to the configurationmanager.psd1 script if it is not at the default location.

.EXAMPLE

PS C:\> New-CMScriptDeployement -CMDrive 'newDrive' -ServerFQDN 'SCCM.testlab.local' -TargetDevice 'target.testlab.local' -Path 'C:\temp\reverse.ps1' -ScriptName 'EvilScript'

.LINK

https://docs.microsoft.com/en-us/powershell/module/configurationmanager/?view=sccm-ps
#>

param(
[Parameter(Mandatory = $True)]
[String]
$CMDrive,

[Parameter(Mandatory = $True)]
[String]
$ServerFQDN,

[Parameter(Mandatory = $True)]
[String]
$TargetDevice,

[Parameter(Mandatory = $True)]
[String]
$Path,

[Parameter(Mandatory = $True)]
[String]
$ScriptName,

[String]
$ManagerPath = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\configurationmanager.psd1"
)

try {
Import-Module $ManagerPath
}
catch {
Write-Warning $_
}

# Create the Configuration Manager drive and move to it
New-PSDrive -Name $CMDrive -PSProvider CMSite -Root $ServerFQDN
Set-Location $CMDrive':\'

# Retrieve the target device
$CMDevice = Get-CMDevice -Name $TargetDevice

# Create a new CM Script and retrieve its GUID
New-CMScript -ScriptFile $Path -ScriptName $ScriptName
$ScriptGuid = (Get-CMScript -ScriptName $ScriptName -Fast).ScriptGuid

# "Commit" the script and execute it on the remote machine
Approve-CMScript -ScriptGuid $ScriptGuid
Invoke-CMScript -ScriptGuid $ScriptGuid -Device $CMDevice -PassThru
}
5 changes: 3 additions & 2 deletions PowerSCCM.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ FunctionsToExport = @(
'Remove-SccmApplicationDeployment',
'Push-WmiPayload',
'Remove-WmiPayload',
'Grant-WmiNameSpaceRead ',
'Revoke-WmiNameSpaceRead'
'Grant-WmiNameSpaceRead',
'Revoke-WmiNameSpaceRead',
'New-CMScriptDeployement'
)

# List of all files packaged with this module
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ Grants remote read access to 'Everyone' for a given WMI namespace.
#### Revoke-WmiNameSpaceRead
Removes remote read access from 'Everyone' for a given WMI namespace that was granted by Grant-WmiNameSpaceRead.

#### New-CMScriptDeployement
Permits to deploy a PowerShell script (called a CMScript) on a distant machine with SCCM, instead of an application.

## Offensive Deployment

Expand Down