Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

PS-Skript "Mit Powershell ausführen" - Funktion mit verpflichtende Parameter

$
0
0
Hallo Zusammen,

ich möchte mittels einem PS Skript, Beispielhaft hier, einen AD Benutzer auswählen und entsprechende Informationen exportieren/öffnen.
Hierzu habe eine PS-Skript mit Funktion erstellt, mit verpflichtenden Parameter, was auch soweit funktioniert (ISE F5/F8) oder als PS-Verknüpfung.

Verständnis Frage:
Ich möchte das Script nun “Mit Powershell ausführen” und dabei soll, wenn nicht erfolgt, die benötigen Eingabewerte abgefragt werden.
Führe ich das Script aus, so schließt es sich direkt wieder…

Geht dies überhaupt (so wie mit der F5 ausführen Funktion der ISE) oder muss hier mit ReadHost (vermutlich in der PARAM Sektion) gearbeitet werden?

CODE:
#<#
.SYNOPSIS
    SELECT a user object from the AD (ACTIVE DIRECTORY)

.DESCRIPTION
    SELECT a user ocject from the AD and export some attribute information:

.NOTES
     Script Name     : SELECT-ADUser
     Description     : SELECT AD User Account

     Version         : 1.0

.REQUIREMENT
    PowerShell Version
        - Version 4
            "(get-host).version.major" or "$PSVersionTable.PSVersion"

    PowerShell Module
        - ActiveDirectory
            "Import-Module ActiveDirectory"

    PowerShell PSSnapin
        - None

    PowerShell Extensions
        - None

    PowerShell Assembly
        - None

    Script Rights
        - The account running the script needs
            RO access to global AD users

.PARAMETER
    - ExportPath "Required Parameter. Example is 'C:\PS\Reports\' Folder"

.EXAMPLE

    - Start script to select AD User Object "m.mustermann" and save the output CSV file to C:\PS\Reports\
        .\SELECT-ADUser -Account "m.mustermann" -ExportPath "C:\PS\Reports\"

.SOURCE
    None

.TEST OPTION
    NONE
#
#>                                                   # SYNOPSIS BLOCK
#
[cmdletbinding()]
PARAM (
        [Parameter(Mandatory=$True,
            ValueFromPipeline=$True,
            ValueFromPipelineByPropertyName=$True,
            HelpMessage="ENTER: 'AD User SamAccountName' like 'm.mustermann'")]
            [ValidateNotNullOrEmpty()]
            [String] $Account,

        [Parameter(Mandatory=$True,
            ValueFromPipeline=$True,
            ValueFromPipelineByPropertyName=$True,
            HelpMessage="ENTER: ExportPath for CSV files like 'C:\PS\Reports\'")]
            [ValidateNotNullOrEmpty()]
            [String] $ExportPath
            )                       # PARAMETER BLOCK
#
BEGIN {
#
#----------------------------------------------------------
# ERROR HANDLING
#----------------------------------------------------------
#
# /* ERROR REPORTING ALL */
Set-StrictMode -Version latest
#
# /* ERROR Action */
$ErrorActionPreference       = "SilentlyContinue"
#
#----------------------------------------------------------
# FUNCTIONS DECLARATION
#----------------------------------------------------------
Function Load-ActiveDirectoryModuleLocal {
    Write-Host "LOAD`t`t'PS Module 'ActiveDirectory''" -ForegroundColor Yellow -nonewline
    IF (!(Get-Module ActiveDirectory -ErrorAction $ErrorActionPreference)) {
            Try {
                Import-Module ActiveDirectory
                }
            Catch {
                Write-Host "`t" -NoNewline
                Write-Warning  "EXIT: 'LOAD Module 'ActiveDirectory' not possible!'"
		        Sleep 3
                Exit 0
                }
            Finally {
                IF (Get-Module ActiveDirectory -ErrorAction $ErrorActionPreference) { Write-Host "`t`tOK`tLOAD" -ForegroundColor Green}
                }
        }
    ELSE {
        Write-Host "`t`tOK`t'Module already loaded'!" -foregroundcolor Green

    }
}            # END Function Load-ActiveDirectoryModuleLocal
#
Function Get-ADCloseGC {
    #Get this computer's AD site using .NET
    $mySite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()
    #Get the DNS Root domain
    $ADRoot = $(Get-AdDomain).DNSRoot
    #Get enabled DCs which are GCs in MySite
    [array]$DomainGCs = Get-ADDomainController -server $ADRoot `
     -Filter {(Enabled -eq $true) -and (IsGlobalCatalog -eq $true)}
    #Get GCs for this computer's AD site
    [array]$MyGCs = $DomainGCs | Where-Object {$_.Site -eq $mySite}
    #Get GC for mySite, if none available, pick first returned from DomainGCs
    IF ($MyGCs -ne $null) {
        Return $MyGCs[0].HostName
        }
    ELSE {
        Return $DomainGCs[0].HostName
        }
}                              # END Function Get-ADCloseGC
#
#----------------------------------------------------------
# GLOBAL SCRIPT VARIABLES
#----------------------------------------------------------
#
$PSScriptVersion             = "v1.0"
$ReportName                  = "SELECT-ADUser"
}
#
PROCESS {
    #
    # /* Clear Screen*/
    Clear-Host
    #
    Write-Host -ForegroundColor Yellow "-------------------------------------------------------------------------------------------"
    Write-Host -ForegroundColor Yellow "START:`t<>`tSCRIPT`t:: '$ReportName' Version '$PSScriptVersion'"
    Write-Host -ForegroundColor Yellow "-------------------------------------------------------------------------------------------"
    #
    #----------------------------------------------------------
    # Load Modules
    #----------------------------------------------------------
	#
    # /* LOAD Modules */
    Load-ActiveDirectoryModuleLocal
	#
    #----------------------------------------------------------
    # SCRIPT VARIABLES
    #----------------------------------------------------------
    #
    $ADCloseGC                   = Get-ADCloseGC
    #
    $efile                       = "$($ExportPath )$($ExportFileName).csv"
    #----------------------------------------------------------
    # SCRIPT SEQUENCE
    #----------------------------------------------------------
    #
    # /* CHECK if username exists */
    $UserCheck               = $(Try {Get-ADUser -Identity $Account -Server $ADCloseGC} Catch {$null})
    If (!$UserCheck) {
    Write-Host -ForegroundColor Red    "CHECK:`t<>`tERROR`t:: SamAccountName not found '$($Account)'"
    Sleep 3
    Exit 0
    }
    ELSE {
    Write-Host -ForegroundColor Green  "CHECK:`t<>`tOK`t`t:: SamAccountName found '$($Account)'"
    }
    #
    # /* GET AD User Object Attributes and Information */
    #
    #----------------------------------------------------------
    # GET Active Directory Information
    #----------------------------------------------------------
    #
    $ADUser                  = Get-ADUser -Identity $Account -Property * -Server $ADCloseGC | Sort-Object
    #
    #----------------------------------------------------------
    # REPORT Section
    #----------------------------------------------------------
    #
    $Result           = @()
    $Object                  = New-Object PSObject
        Add-Member -InputObject $Object -memberType NoteProperty -Name DisplayName        -Value $ADUser.'DisplayName'
        Add-Member -InputObject $Object -memberType NoteProperty -Name SamAccountName     -Value $ADUser.'SamAccountName'
        Add-Member -InputObject $Object -memberType NoteProperty -Name SID                -Value $ADUser.'objectSID'.Value
    $Result += $Object
    #
   }
#
END {
    #----------------------------------------------------------
    # Export CSV
    #----------------------------------------------------------
    Write-Host "EXPORT:`t`t'Results to CSV-File'" -ForegroundColor Yellow -nonewline
    #
    $Result                 | Export-Csv -Path $efile -Delimiter ";" -Encoding UTF8 -NoTypeInformation -ErrorAction $ErrorActionPreference& $efile
    #
    IF ($?) {
        Write-Host "`t`t`t`tOK" -Foreground Green
        }
    ELSE {
        Write-Host ""
        Write-Warning "`tERROR in Section 'CSV File Export'"
        }
    #
    Exit 0
    }
#

Vielen Dank im Voraus für jede Art der Zusammenarbeit.


Manfred Schüler


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>