locked
powershell STA mode SCSM 2012 RRS feed

  • Question

  • Hi

    How to use powershell STA mode for a script in SCSM 2012 while creating a task in library?


    Regards, Darshan G. Parab

    Saturday, December 8, 2012 1:11 PM

All replies

  • Hi,

    isn't it to just add the -STA switch?


    Peter Forster | MVP Virtual Machine 2002-2011 | Austria |

    Saturday, December 8, 2012 1:49 PM
  • We tried that, but its not working. We have written one script. When i start powershell with STA mode and run the script then it works fine. But when I do the  same thing in SCSM 2012, it starts throwing error.

    Regards, Darshan G. Parab


    Saturday, December 8, 2012 1:52 PM
  • Hi,

    can you post your task configuration?


    Peter Forster | MVP Virtual Machine 2002-2011 | Austria |

    Saturday, December 8, 2012 6:24 PM
  • Hi Peter,

    Find below the script that we are using to send E-mails. File alluser.csv contains E-mail addresses for look-up in the form.

    ------------------------------------------------------------------------------------------------------------------------------------------

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][System.Windows.Forms.Application]::EnableVisualStyles()
    [void][System.Windows.Forms.ComboBoxStyle]::DropDown
    $allmailids = $null | Out-Null

    $objForm = New-Object system.windows.forms.form
    $objForm.Text = "Send E-mail to user"
    $objForm.size = New-Object system.drawing.size(500,700)
    $objForm.Startposition = "CenterScreen"

    $objLabelCC1 = New-object System.Windows.Forms.Label
    $objLabelCC1.Size = New-Object System.Drawing.Size(40,20)
    $objLabelCC1.Location = New-Object System.Drawing.Size(10,20)
    $objLabelCC1.Text = "CC : "
    $objForm.Controls.Add($objLabelCC1)

    $objTextBoxCC1 = New-Object System.Windows.Forms.textBox
    $objTextBoxCC1.Size = New-Object System.Drawing.Size(400,20)
    $objTextBoxCC1.Location = New-Object System.Drawing.Size(50,20)
    $objForm.Controls.Add($objTextBoxCC1)

    $objDropDownCC1 = New-Object System.Windows.Forms.ComboBox
    $objDropDownCC1.size = New-Object System.Drawing.size(400,20)
    $objDropDownCC1.Location = New-Object system.Drawing.Size(50,60)
    $objDropDownCC1.AutoCompleteCustomSource.Add("System.Windows.Forms");
    $objDropDownCC1.AutoCompleteCustomSource.AddRange((%{get-content .\Allusers.csv}))
    $objDropDownCC1.AutoCompleteMode = [System.Windows.Forms.AutoCompleteMode]::SuggestAppend;
    $objDropDownCC1.AutoCompleteSource = [System.Windows.Forms.AutoCompleteSource]::CustomSource;
    $objForm.Controls.Add($objDropDownCC1)

    $objButtonAddmailid = New-Object System.Windows.Forms.Button
    $objButtonAddmailid.size = New-Object System.Drawing.size(40,20)
    $objButtonAddmailid.location = New-object System.Drawing.size(10,60)
    $objButtonAddmailid.Text = "Add E-mail ID"
    $objButtonAddmailid.Add_click({$objTextBoxCC1.Text=$objTextBoxCC1.Text+$objDropDownCC1.Text+";";$objDropDownCC1.Text = $null})
    $objForm.Controls.Add($objButtonAddmailid)

    $objButtonOK = New-Object System.Windows.Forms.Button
    $objButtonOK.Text = "OK"
    $objButtonOK.size = New-object System.Drawing.size(40,20)
    $objButtonOK.Location = New-Object System.Drawing.size(10,100)
    $objButtonOK.Add_Click({$cc = $objTextBoxCC1.Text;$objForm.Close();$cc=$cc.split(";");$cc=$cc[0..($cc.Count-2)];Invoke-command -computername scsmadminserver {param([string[]]$allcc);Send-MailMessage -to "darshan.parab@gmail.com" -subject "Hi" -body "Text msg..." -from "test.admin@mydomain.com" -cc $allcc  -smtpserver smtp.mydomain.com} -Argumentlist (,$cc)})
    $objForm.Controls.Add($objButtonOK)
    $objForm.TopMost = $true
    $objForm.Add_shown({$objForm.Activate()})
    [void]$objForm.ShowDialog()
    ------------------------------------------------------------------------------------------------------------------------------------------


    Regards, Darshan G. Parab

    Monday, December 10, 2012 7:13 AM