# Invoke-Command wrapper function
A quick post this time about something that might be helpful for others, something that saves me from typing too much ๐
I've noticed over the last week that I've been doing various remoting commands through Invoke-Command to the same machines, which require additional credentials to access.
Why re-type something when you can automate it ๐
# Before
I used to have to do the following:
```powershell
$SCCMServers = 'CM01.contoso.com','CM02.contoso.com','CM03.contoso.com','CM04.contoso.com'
$cred_SCCM = Import-CliXml $CredStore\contoso.xml
Invoke-Command -ComputerName $SCCMServers -Credential $cred_SCCM -Scriptblock {whatever needed to be done}
```
Where my SCCM servers are in another forest as my management machine and $CredStore is my personal Credential folder which contains credentials for various systems/domains, as I use these regularly and I'm lazy
# After
After some tinkering around and a great tip found on Thomas Maurer's site, I've narrowed it down to:
```powershell
Invoke-SCCMCommand -Script 'whatever I need to have done here'
```
I've personally decided to keep the 2 variables in my $profile as I tend to use those for other functions as well, but otherwise I would recommend placing these in the begin {} block.
The magic here is the converting of a string to an actual scriptblock by the PowerShell engine, accomplished by the following piece of code:
```powershell
$ScriptBlock = [Scriptblock]::Create($Script)
```
# The Code
```powershell
function Invoke-SCCMCommand {
<#
.SYNOPSIS
Wrapper function for Invoke-Command with specific parameters.
.DESCRIPTION
Wrapper function for Invoke-Command with specific parameters.
Requires the SCCMServer and Cred_SCCM variables to be present.
.PARAMETER Script
Provide the script to be run on all SCCM machines
.EXAMPLE
PS C:\> Invoke-SCCMCommand -script 'Test-Connection -Name google.com'
Runs a Test-Connection from all SCCM machines.
.NOTES
Created by: Robert Prรผst
Blog: http://powershellpr0mpt.com
Version: 1.0
#>
[cmdletBinding()]
param (
[Parameter(Mandatory=$true)]
$Script
)
begin {
Write-Verbose "Script to run is '$Script'"
Write-Verbose 'Converting script to actual scriptblock'
$ScriptBlock = [Scriptblock]::Create($Script)
}
process {
Invoke-Command -ComputerName $SCCMServers -Credential $cred_SCCM -ScriptBlock $ScriptBlock
}
}
```
Happy Scripting! ๐