流程执行

时间:2017-02-27 12:40:40

标签: powershell csv

我不是Powershell的新手,但我不熟悉自己在Powershell制作脚本。

我必须制作一个简单的脚本,让我们的所有员工都在工作,并提供有关它们的一些信息,然后输出包含此信息的CSV文件。但是当我运行下面的代码时,没有任何反应。 ISE只是执行该过程,第二次通过并完成。然后没有创建CSV文件。

PROCESS {
    $path = Split-Path -parent "C:\Users\omitted\Desktop"
    $pathexist = Test-Path -Path $path
    If($pathexist -eq $false) {
        New-IOItem -type directory -Path $path
    }
    $csvreportfile = $path + "\ALLADUsers_.csv"

    #import the AD Module
    Import-Module ActiveDirectory

    Get-ADUser -SearchBase "OU=Medarbejdere,OU=Users,OU=omitted,DC=omitted,DC=local" -Properties MemberOf -Filter * |
        Select-Object @{ Label = "First Name";   Expression = { $_.GivenName } },
                      @{ Label = "Last Name";    Expression = { $_.Surname } },
                      @{ Label = "Display Name"; Expression = { $_.DisplayName } },
                      @{ Label = "Logon Name";   Expression = { $_.SamAccountName } },
                      @{ Label = "Job Title";    Expression = { $_.Title } },
                      @{ Label = "Description";  Expression = { $_.Description } },
                      @{ Label = "Department";   Expression = { $_.Department } } |

        #Export CSV Report
        Export-Csv -Path $csvreportfile -NoTypeInformation
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个:

#moved the Parent parameter to the end to more clearly distinguish Path's value from Parent (switch).  
$path = Split-Path -Path "C:\Users\omitted\Desktop" -Parent 

#Since we don't refer to $PathExist again, skipped the assignment and put the test directly in the condition
If(Test-Path -Path $path) { 
    #Corrected Type; was `New-IOItem`
    New-Item -type directory -Path $path
}

#use Join-Path when joining paths, to avoid issues with too many/too few slashes
$csvreportfile = Join-Path $path "ALLADUsers_.csv"

#import the AD Module
Import-Module ActiveDirectory

#Added other properties which wouldn't be returned by default (see https://social.technet.microsoft.com/wiki/contents/articles/12037.active-directory-get-aduser-default-and-extended-properties.aspx for a list of all properties / those in Cyan are the only ones which don't need to be listed on the Properties parameter)
Get-ADUser -SearchBase "OU=Medarbejdere,OU=Users,OU=omitted,DC=omitted,DC=local" -Properties MemberOf, Title, Description, Department -Filter * |
    Select-Object @{ Label = "First Name";   Expression = { $_.GivenName } },
                  @{ Label = "Last Name";    Expression = { $_.Surname } },
                  @{ Label = "Display Name"; Expression = { $_.DisplayName } },
                  @{ Label = "Logon Name";   Expression = { $_.SamAccountName } },
                  @{ Label = "Job Title";    Expression = { $_.Title } },
                  @{ Label = "Description";  Expression = { $_.Description } },
                  @{ Label = "Department";   Expression = { $_.Department } } `
| Export-Csv -Path $csvreportfile -NoTypeInformation #moved this so that the pipeline feeds into export-csv; the whitespace had made this statement invalid previously

我已添加评论内联解释我所做的更改。

关于PROCESS部分,如果您直接在脚本体中运行此部分,则不需要这样做。如果我定义了一个函数/ cmdlet,我通常会包含这个(请参阅https://ss64.com/ps/syntax-function-input.html)。