none
1 Function in 2 scripten .. RRS feed

  • Frage

  • Hallo, ich habe eine function in einem script ausgearbeitet, funktioniert einwandfrei.

    Nun habe ich diese function in in ein script eingearbeitet welches die Bedienung erleichtert.

    Dort wirft diese einen Fehler

    Script1 (funktioniert)

    Clear-Host 
    #import-module activedirectory 
    ##cd ad: 
    #dir 
      #cd D:\PS_Scripts 
       $sikst = @(Get-Content .\KST.txt) 
     
     
    function get-KST_user { 
    if($excel){   $excel.Application.DisplayAlerts = $false 
      $excel.quit()} 
      $ExcelProcess=get-process excel 
      $ExcelProcess | foreach {stop-process ($_.id)} 
     
      #[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | out-null
    
     
       $excel = new-object -comobject excel.application # ComObjekt erstellen#
    
     
       $excel.visible = $true 
       $workbook=$excel.Workbooks.add() 
       $sheet = $workbook.Worksheets.Item("Tabelle1") 
     
       $sheet.Cells.Item(1,2).value2="User ID" 
       $sheet.Cells.Item(1,3).value2="Vorname" 
       $sheet.Cells.Item(1,4).value2="Nachname" 
       $sheet.Cells.Item(1,5).value2="Kostenstelle" 
       $sheet.Cells.Item(1,6).value2="voller Name" 
       $sheet.Cells.Item(1,7).value2="E-Mail" 
       $sheet.Cells.Item(1,8).value2="Aktiv" 
       $sheet.range($sheet.cells.item(1,2),$sheet.cells.item(1,8)).Font.Bold = $true##
    
     
       $i=2 
    Get-aduser -Properties displayname,extensionattribute2,mail  -filter * -SearchBase "OU=$kst,OU=xxx,OU=Domain Users,DC=xx,DC=xx" | where { $_.enabled -like "true" -and $_.extensionattribute2 -notlike "0998" -and $_.displayname
     -notlike "*xxx*" -and $_.surname -notlike "*R:*"} | Select-Object name,surname,givenname,enabled,extensionattribute2,displayname,mail | foreach {
    
       $sheet.Cells.Item($i,2).value2="{0}" -f $_.name 
       $sheet.Cells.Item($i,3).value2="{0}" -f $_.givenname 
       $sheet.Cells.Item($i,4).value2="{0}" -f $_.surname 
       $sheet.Cells.Item($i,5).value2="{0}" -f $_.extensionattribute2
    
       $sheet.Cells.Item($i,6).value2="{0}" -f $_.displayname 
       $sheet.Cells.Item($i,7).value2="{0}" -f $_.mail 
       $sheet.Cells.Item($i,8).value2="{0:f}" -f $_.enabled 
           
       $i++ 
       } 
       $range = $sheet.usedRange 
       $range.EntireColumn.AutoFit() | out-null  #autom. Anpassen der Spaltenbreiten
    
     
    } 
     
     
    $Exit = $false 
    $found = $false 
     
    do 
    { 
                 $kst = Read-Host "Bitte Kostenstelle der xxx eingeben"
    
     
           if ($kst -eq "Exit") 
           { 
                   $Exit = $true
    
                   break #Bricht den aktuellen durchlauf ab
    
           } 
           else 
           { 
     
    #$found = $false 
    foreach($akst in $sikst){ 
           if($akst -eq $kst) 
           { 
                   $found = $true
    
               get-KST_user 
           } 
    } 
     
           if($found) 
           {   
               "Exceltabbele für $kst wurde erstellt"
    
           } 
           else 
           { 
               "Falsche Eingabe"
    
               break 
    } 
           } 
           
    } 
    while ($Exit) 
    
    $strResponse = “Quit” 
     do {$strResponse = Read-Host “Abfrage beeneden ? (Y/N)”}
                    until ($strResponse -eq “Y”)
    
    clear-host



    Script 2 (Fehler)

    #import-module activedirectory 
    #cd D:\PS_Scripts
    
    
    
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("system.windows.forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.windows.drawing")
    
     #if ($kst -notlike "null") {Clear-Variable -Name kst}
    
        $cost = @(0870,0871,0872,0873,0874)
    
    
    function get-KST_user { 
    if($excel){   $excel.Application.DisplayAlerts = $false 
      $excel.quit()} 
      $ExcelProcess=get-process excel 
      $ExcelProcess | foreach {stop-process ($_.id)} 
     
      #[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | out-null
    
     
       $excel = new-object -comobject excel.application # ComObjekt erstellen#
    
     
       $excel.visible = $true 
       $workbook=$excel.Workbooks.add() 
       $sheet = $workbook.Worksheets.Item("Tabelle1") 
     
       $sheet.Cells.Item(1,2).value2="User ID" 
       $sheet.Cells.Item(1,3).value2="Vorname" 
       $sheet.Cells.Item(1,4).value2="Nachname" 
       $sheet.Cells.Item(1,5).value2="Kostenstelle" 
       $sheet.Cells.Item(1,6).value2="voller Name" 
       $sheet.Cells.Item(1,7).value2="E-Mail" 
       $sheet.Cells.Item(1,8).value2="Aktiv" 
       $sheet.range($sheet.cells.item(1,2),$sheet.cells.item(1,8)).Font.Bold = $true##
    
     
       $i=2 
    Get-aduser -Properties displayname,extensionattribute2,mail  -filter * -SearchBase "OU=$kst,OU=xxx,OU=Domain Users,DC=xx,DC=xx" | where { $_.enabled -like "true" -and $_.extensionattribute2 -notlike "0998" -and $_.displayname
     -notlike "*xxx*" -and $_.surname -notlike "*R:*"} | Select-Object name,surname,givenname,enabled,extensionattribute2,displayname,mail | foreach {
    
       $sheet.Cells.Item($i,2).value2="{0}" -f $_.name 
       $sheet.Cells.Item($i,3).value2="{0}" -f $_.givenname 
       $sheet.Cells.Item($i,4).value2="{0}" -f $_.surname 
       $sheet.Cells.Item($i,5).value2="{0}" -f $_.extensionattribute2
    
       $sheet.Cells.Item($i,6).value2="{0}" -f $_.displayname 
       $sheet.Cells.Item($i,7).value2="{0}" -f $_.mail 
       $sheet.Cells.Item($i,8).value2="{0:f}" -f $_.enabled 
           
       $i++ 
       } 
       $range = $sheet.usedRange 
       $range.EntireColumn.AutoFit() | out-null  #autom. Anpassen der Spaltenbreiten
    
     
    } 
    
     
     #Auswahlbox
    
    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.ClientSize = New-Object System.Drawing.Size(407, 390)
    $form1.topmost = $true
    
    
    
    $comboBox1 = New-Object System.Windows.Forms.ComboBox
    $comboBox1.Location = New-Object System.Drawing.Point(25, 55)
    $comboBox1.Size = New-Object System.Drawing.Size(350, 310)
    $comboBox1.Text = "Auswählen ..."
        foreach($acost in $cost)
            {
                $comboBox1.Items.add($acost)
            }
                $Form1.Controls.Add($comboBox1)
    
    
    
    
    
    
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Point(25, 20)
    $Button.Size = New-Object System.Drawing.Size(98, 23)
    $Button.Text = "Output"
    $Button.add_Click({get-KST_user})
    $Button.add_Click({$label.Text =  $comboBox1.SelectedItem.ToString()})
    $Form1.Controls.Add($Button)
    
    
    
    
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(70, 90)
    $label.Size = New-Object System.Drawing.Size(98, 23)
    $label.Text = "$kst"
    $Form1.Controls.Add($label)
    
    
    
    [void]$form1.showdialog()
    
    $kst = $comboBox1.SelectedItem
    
     #if ($kst -notlike "null") {Clear-Variable -Name kst}

    Fehlermeldung

    Get-aduser : Die Syntax des Objektnamens ist ungültig
    In D:\PS_Scripts\combo_kst.ps1:41 Zeichen:1
    + Get-aduser -Properties displayname,extensionattribute2,mail  -filter * -SearchBa ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
        + FullyQualifiedErrorId : Die Syntax des Objektnamens ist ungültig,Microsoft.ActiveDirectory.Management.Commands.GetADUse
       r

    Ich habe schon graue Haare und weiss nicht woran es liegt ^^

    Gruß



    • Bearbeitet Geo_Uma Dienstag, 5. November 2013 06:25
    Montag, 4. November 2013 13:53

Antworten

Alle Antworten

  • Ha ha ......

    Ich Sitze im selben Active Directory wir Du !
    Gib in deinem Outlook mal "Kriegel, Peter" ein  und schick mir eine Mail an meine Normal Adresse ! ;-))

    Dein Fehler ist du generierst dein Array als Array von Zahlen!

    $cost = @(0870,0871,0872,0873,0874)

    Bei Zahlen werden die Führenden Nullen von deinen Kostenstellen abgeschnitten!

    Du musst die Kostenstellen als Text mit Anführungszeichen definieren!

    $cost = @('0870','0871','0872','0873','0874')


    Meine PowerShell Artikel, Buchtipps und kostenlose PowerShell Tutorials + E-Books
    Mein deutscher PowerShell Blog
    Mein 21 Teiliger PowerShell Video Grundlehrgang
    Deutsche PowerShell Videos auf Youtube
    Folge mir auf:
    Twitter | Facebook | Google+ | Deutsches PowerShell Forum (TechNet)

    • Bearbeitet Peter Kriegel Dienstag, 5. November 2013 07:20
    • Als Antwort markiert Geo_Uma Dienstag, 5. November 2013 10:23
    Montag, 4. November 2013 15:28
  • Wie selbe AD ??

    Leider war das nicht des Rätsels Lösung ^^

    Ich hatte schon die Vermutung das es mit der Reihenfolge im Script zutun hat und verschiedenes getestet .. leider ohne Erfolg  :-(

    • Bearbeitet Geo_Uma Dienstag, 5. November 2013 06:23
    Montag, 4. November 2013 17:38
  • Wie selbe AD ??

    Stehst du auf dem Schlauch? Ihr baut für uns Sitze ;-)

    Leider war das nicht des Rätsels Lösung ^^

    Ich habe das in unserem AD getestet, mit euren Kostenstellen.
    bei mir läuft es mit dieser Änderung!

    Gib in deinem dienstlichen Outlook mal "Kriegel, Peter" ein  und schick mir eine Mail an meine Normal Adresse ! ;-))


    Meine PowerShell Artikel, Buchtipps und kostenlose PowerShell Tutorials + E-Books
    Mein deutscher PowerShell Blog
    Mein 21 Teiliger PowerShell Video Grundlehrgang
    Deutsche PowerShell Videos auf Youtube
    Folge mir auf:
    Twitter | Facebook | Google+ | Deutsches PowerShell Forum (TechNet)

    • Als Antwort markiert Geo_Uma Dienstag, 5. November 2013 10:24
    Dienstag, 5. November 2013 07:23