Module, cmdlet and Commands

Tagged: , ,

This topic contains 8 replies, has 4 voices, and was last updated by  Knut 8 months, 2 weeks ago.

  • Author
    Posts
  • #61194

    Knut
    Participant

    Hey there 🙂
    I got another problem:
    I created a Script:

    # Active Directory Modul laden
    Import-Module ActiveDirectory
    
    # Gruppe abfragen
    param([Parameter(Mandatory=$true)]
         [String]$Group)
     
    # Active Directory Modul laden
    Import-Module ActiveDirectory
     
    echo $Group
     
    $title = "Welche Vorlage möchten Sie verwenden?"
    $message = "1.Neue_Störung  2.Update_Störung  3.Störungsabschluss 4.Hinweismeldung"
    $eins = New-Object System.Management.Automation.Host.ChoiceDescription "&1", "1"
    $zwei = New-Object System.Management.Automation.Host.ChoiceDescription "&2", "2"
    $drei = New-Object System.Management.Automation.Host.ChoiceDescription "&3", "3"
    $vier = New-Object System.Management.Automation.Host.ChoiceDescription "&4", "4"
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($eins, $zwei,$drei,$vier)
    $choice_groups=$host.ui.PromptForChoice($title, $message, $options, 1)
    
    
    If ($choice_groups -eq 0){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\01_Neue Störung.oft"
    }
    
    If ($choice_groups -eq 1){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\02_Update Störung.oft"
    }
    
    If ($choice_groups -eq 2){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\03_Störungabschluss.oft"
    }
    
    If ($choice_groups -eq 3){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\00_[Hinweismeldung].oft"
    }
    
     
    # Email-Adressen ermitteln
    $EmailAddresses = Get-ADGroupMember $Group -Recursive | Get-ADUser -Properties mail | Select mail
    echo $EmailAddresses
    ForEach ($EmailAddress In $EmailAddresses){
      $OLAddresses = $EmailAddress.mail + ";" + $OLAddresses
    }
     
    # Outlookmail erstellen
    $OL = New-Object -comObject Outlook.Application  
    $Mail = $OL.CreateItemFromTemplate($path)
    $Mail.TO = $OLAddresses
    #$Mail.BCC = $OLAddresses
    $Mail.Display()
    

    Everything is fine when I start it on my device. But when I start it on another device it returns an error that "param" is not a name of a cmdlet , function or a scriptfile.

    Now my question. Why does it work on my device but nowhere else. Is there something I have to activate?

    Greetings
    Knutonier

  • #61197

    Don Jones
    Keymaster

    It may be a difference in PowerShell versions. But, you want the param() block to be the first thing in the script, and presently it isn't.

    • #61200

      Knut
      Participant

      Both devices have the PowerShell V4.0. I put the param() block at first. Doesn't work.

  • #61201

    Knut
    Participant

    Well the first two lines are on the wrong place. When I start the Script in the ISE it works on every device. But when I rightclick it and say "open with PowerShell" it now works nowhere. The PowerShell-Window popsup and disappear.

    # Gruppe abfragen
    param(
         [Parameter(Mandatory=$true)]
         [String]$Group)
    
     
    # Active Directory Modul laden
    Import-Module ActiveDirectory
     
    echo $Group
     
    $title = "Welche Vorlage möchten Sie verwenden?"
    $message = "1.Neue_Störung  2.Update_Störung  3.Störungsabschluss 4.Hinweismeldung"
    $eins = New-Object System.Management.Automation.Host.ChoiceDescription "&1", "1"
    $zwei = New-Object System.Management.Automation.Host.ChoiceDescription "&2", "2"
    $drei = New-Object System.Management.Automation.Host.ChoiceDescription "&3", "3"
    $vier = New-Object System.Management.Automation.Host.ChoiceDescription "&4", "4"
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($eins, $zwei,$drei,$vier)
    $choice_groups=$host.ui.PromptForChoice($title, $message, $options, 1)
    
    
    If ($choice_groups -eq 0){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\01_Neue Störung.oft"
    }
    
    If ($choice_groups -eq 1){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\02_Update Störung.oft"
    }
    
    If ($choice_groups -eq 2){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\03_Störungabschluss.oft"
    }
    
    If ($choice_groups -eq 3){
    $path = "H:\edv\IT_Operations\IT- Service Koordination\02_Eskalationsmanagement\00_Vorlagen zu Eskalationen\000_Muster und Vorlagen\00_[Hinweismeldung].oft"
    }
    
     
    # Email-Adressen ermitteln
    $EmailAddresses = Get-ADGroupMember $Group -Recursive | Get-ADUser -Properties mail | Select mail
    echo $EmailAddresses
    ForEach ($EmailAddress In $EmailAddresses){
      $OLAddresses = $EmailAddress.mail + ";" + $OLAddresses
    }
     
    # Outlookmail erstellen
    $OL = New-Object -comObject Outlook.Application  
    $Mail = $OL.CreateItemFromTemplate($path)
    $Mail.TO = $OLAddresses
    #$Mail.BCC = $OLAddresses
    $Mail.Display()
    
  • #61203

    Aaron Hardy
    Participant

    I'm on the PowerShell journey myself but I did notice something at the beginning of your code.

    You are specifying a parameter (Group) that is mandatory which is fine but how are you populating a value into that parameter?

    If you want to use your script as a cmdlet (advanced function), it would be much easier to make it a module, then you can easily specify the parameter name. Otherwise you have to dot-source the script file each time you want to run it then call the function with the parameter and its value.

    [CmdletBinding()]
    param(
         [Parameter(Mandatory=$true)]
         [String]$Group)
    

    If this is not relative, forgive my ignorance.

    Edit: It's best to test in a PS console, not in ISE (though convenient at times).

  • #61210

    Ron
    Participant

    I can't be more specific and I'm guessing, I cheat and use a GUI tool, but most likely the problem is that some Forms assemblies need to be loaded when running from the console. ISE loads them automatically.

    • #61369

      Knut
      Participant

      How can I load these Forms when running from console? Could you give me an example?

  • #61372

    Knut
    Participant

    Well now I just opened the PowerShel Console and drag and drop my ".ps1" into it. That works :O But when I rightclick it and say "open with PowerShell" it doesn't work. Where is the difference between these two procedures?

  • #61395

    Knut
    Participant

    I found the problem. When i use rightclick and say "open with PowerShell" it uses another console then I open for the drag and drop thing. I changed it and it works. Thanks to everyone 🙂

You must be logged in to reply to this topic.