Run Scripts with Parameters in MEMCM (R.I.P. SCCM)

How do you use Parameters in MEMCM's (R.I.P. SCCM) Run Scripts?

How do you use Parameters on the Run Scripts feature? You get three guesses, but the first two don't count. That's right, you create a script with standard PowerShell Parameters and you build it out in MEMCM. In your MEMCM console, navigate to Software Library > Overview > Scripts. If you're not familiar with Run Scripts in MEMCM, check out my previous Blog articles on it.

Click on Create Script. In the Create Script Wizard that pops up input a Script Name, pick between PowerShell or PowerShell in your Script Language, import or paste in your script with Parameters, and click on Next.

My demo script for this article (scroll alllll the way down, it's at the bottom) is about an 8 out of 10 on the cool factor, and about a 3 out of 10 on the usefulness factor. Long story short, the script will make a computer talk. There are two Parameters that you can input, one for "what do you want to say?" and the other for "who is your message from?" both of which are spoken. The script sets a computer's volume to 80%, says what time it is, makes a snarky remark about the time of day, says your message (the first parameter), and then it says who the message is from (the second parameter).

The next screen in the Create Scripts Wizard shows you the Parameters from your script. You can highlight a Parameter and click on Edit if you want to change anything on it.

You can edit any of the Parameter properties, including those you did or did not specify when you originally wrote the script. I find it best to input a "Friendly Name" for my Parameter and also specify whether it is "Required" to be filled in. Once you get it perfect, click OK

If you edited your Parameter properties, you'll see things like the friendly name reflected back on the Create Scripts Wizard. Once you're ready click on Next, Next, and Finish.

You'll still need to Approve your script before you can use it. Right click on your script and click Approve/Deny. Follow the Approve or Deny Script Wizard to approve your script. If you can't Approve/Deny the script yourself or you need additional info on the process, go to my previous article on CMPivot and Run Scripts, and scroll down to the part about creating and approving scripts.

To test out your script, find some unsuspecting coworker with a sense of humor, right click on their Device, and click on Run Script

In the Run Script Wizard, highlight your script and click on Next. You'll then see where to input your Parameters on the next page of the wizard. Click in the Value fields to fill in your message and who it's from. Once you're ready to go, click on Next

Check the Summary really quick to make sure everything looks good, click Next, and wait for laughter to ensue. 

Full Script

# Windows 10 Text-to-Speech Example

# Create and input a custom parameter for what you want to say
Param(
[Parameter(Mandatory=$True)]
[string]$WhatToSay,
[Parameter(Mandatory=$True)]
[string]$WhoSaidIt
)

#Sets volume to 90% - This section of the script is from Joseph Moody @ DeployHappiness.com, https://deployhappiness.com/setting-sound-volume-unmuting-powershell/
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;

[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
  // f(), g(), ... are unused COM method slots. Define these if you care
  int f(); int g(); int h(); int i();
  int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
  int j();
  int GetMasterVolumeLevelScalar(out float pfLevel);
  int k(); int l(); int m(); int n();
  int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
  int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
  int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
  int f(); // Unused
  int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }

public class Audio {
  static IAudioEndpointVolume Vol() {
    var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
    IMMDevice dev = null;
    Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
    IAudioEndpointVolume epv = null;
    var epvid = typeof(IAudioEndpointVolume).GUID;
    Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
    return epv;
  }
  public static float Volume {
    get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
    set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
  }
  public static bool Mute {
    get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
    set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
  }
}
'@
[ audio ]::Mute = $false
[ audio ]::Volume  = 0.9


# Call up your speech assembly
Add-Type -AssemblyName System.Speech
$TalkToMeGoose = New-Object System.Speech.Synthesis.SpeechSynthesizer
# $TalkToMeGoose.SelectVoice("Microsoft Zira Desktop")
$TalkToMeGoose.SelectVoice("Microsoft David Desktop")
$TalkToMeGoose.Volume = 100

# Time of day stuff
$CurrentDateTime = Get-Date
$CurrentTime = (get-date).ToString('t')
$EarlyMorningEnd = Get-Date -Hour 7
$MorningEnd = Get-Date -Hour 10 -Minute 30
$LunchTimeEnd = Get-Date -Hour 12 -Minute 30
$AfternoonEnd = Get-Date -Hour 17
$BedTime = Get-Date -Hour 20

# Static line of text with a variable of the date / time to speak
$TalkToMeGoose.Speak("Hello, the current time is $CurrentTime")

# If ElseIfs to say something based on Time Of Day
If ($CurrentDateTime.TimeOfDay -lt $EarlyMorningEnd.TimeOfDay) {$TalkToMeGoose.Speak("Sorry to bother you so early chief.")}
ElseIf ($CurrentDateTime.TimeOfDay -lt $MorningEnd.TimeOfDay) {$TalkToMeGoose.Speak("Hope your coffee has had a chance to kick in!")}
ElseIf ($CurrentDateTime.TimeOfDay -lt $LunchTimeEnd.TimeOfDay) {$TalkToMeGoose.Speak("I bet your stomach is growling louder than a pack of hyenas before chow time.")}
ElseIf ($CurrentDateTime.TimeOfDay -lt $AfternoonEnd.TimeOfDay) {$TalkToMeGoose.Speak("Have any big plans for Happy Hour tonight?")}
ElseIf ($CurrentDateTime.TimeOfDay -lt $BedTime.TimeOfDay) {$TalkToMeGoose.Speak("You better start thinking about your dinner plans.")}
ElseIf ($CurrentDateTime.TimeOfDay -gt $BedTime.TimeOfDay) {$TalkToMeGoose.Speak("Isn't it past your bed time?")}

# Static line of text to speak
$TalkToMeGoose.Speak("Please standby for an important message.")

# Variable line of text to speak based on your parameter input
$TalkToMeGoose.Speak($WhatToSay)
$TalkToMeGoose.Speak("This message was sent to you by $WhoSaidIt.")

#The End
$TalkToMeGoose.Dispose()

Comments

Popular posts from this blog

CMPivot to check Services and start them (with a little help)

SCCM CMPivot and Run Scripts