2017-02-27 29 views
0

我對Powershell並不陌生,但我自己對PowerShell中的腳本不熟悉。流程執行後沒有創建CSV文件

我不得不製作一個簡單的腳本,它將我們所有的員工工作並獲得一些關於他們的信息,然後輸出一個包含此信息的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 
} 

我做錯了什麼?

+0

我覺得'Label'應該替換爲'Name',一開始。什麼是「New-IOItem」?我相信你可以使用'New-Item'。你在哪裏找到這個代碼?它看起來有點老。 – sodawillow

+0

我不知道爲什麼你有一個PROCESS塊,但我懷疑這個問題更可能是管道是空的,一旦你到達Export-CSV。嘗試回到管道中並以塊的形式在控制檯中運行。例如,刪除Export-CSV部分,看看你是否得到結果,如果不刪除管道的選擇對象,看看你是否得到結果.. –

+0

我認爲「Label」實際上是一個可以接受的選擇,儘管Name是首選標準以及文檔中的內容。 –

回答

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)。

+1

是的,那是成功的。謝謝。我現在看到了這個錯誤:') – OmniOwl