簡體   English   中英

ForEach-Object $ _變量未按預期工作

[英]ForEach-Object $_ Variable not working as expected

我試圖獲取廣告組中每個用戶的真實姓名,我已經創建了它,以獲取輸出以顯示該組中每個成員的用戶ID,但隨后嘗試使用net user $_ /domain在.txt文件中的每個用戶上,它給我的錯誤是就像那里什么都沒有。 在“為什么不使用Get-AD *”猛烈沖擊之前,我知道它在那里,但是我們不能在這里安裝該模塊。 我需要其他人無需安裝即可使用的東西。

我已經嘗試了幾種方法來解決這個問題,下面的代碼是腳本的一部分,似乎無法正常工作。 我用另一種方式嘗試將其注釋掉,但將其留在此處作為另一個可能的起點。

function Return-DropDown {
    $Choice = $DropDown.SelectedItem.ToString()
    if ($Choice)
        {
        net group $Choice /domain > C:\Temp\RawOut.txt

        $UserID = Get-Content 'C:\Temp\RawOut.txt'
        $UserID[($UserID.IndexOf('-------------------------------------------------------------------------------') + 1) .. ($UserID.IndexOf('The command completed successfully.') -1)] > C:\Temp\RawIDs.txt

        Start C:\Temp\RawIDs.txt

        Remove-Item C:\Temp\RawOut.txt

        Get-Content -path C:\Temp\RawIDs.txt | ForEach-Object {net user $_ /domain | findstr "Full Name"} >> C:\Temp\$Choice+RealNames.txt
        #$RealNames = Get-Content -path C:\Temp\RawIDs.txt
        #ForEach ($_ in $RealNames) 
        #{
           # net user $_ /domain >> C:\Temp\$Choice+1.txt

           # }

        }

}

我得到的是:

net : The syntax of this command is:
At C:\Users\MyID\OneDrive - MyCompany\AD Group DropDown.ps1:34 char:64
+ ... path C:\Temp\RawIDs.txt | ForEach-Object {net user $_ /domain} >> C:\ ...
+                                               ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (The syntax of this command is::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

NET USER
[username [password | *] [options]] [/DOMAIN]
         username {password | *} /ADD [options] [/DOMAIN]
         username [/DELETE] [/DOMAIN]
         username [/TIMES:{times | ALL}]
         username [/ACTIVE: {YES | NO}]

讓我知道有關使此功能正常工作的任何建議。

感謝@LotPings幫助我尋找解決方案。

我發現(通過查看LotPings發表的評論),我得到的輸出格式會導致錯誤。 我的txt文件已格式化:

User1               User2               User3
User4               User5               User6
...

並且它試圖將整行User1 User2 User3作為變量。 這顯然是行不通的。 使用-replace-split為我的變量獲取正確的格式,它現在可以正常工作。

function Return-DropDown {
    $Choice = $DropDown.SelectedItem.ToString()
    if ($Choice)
        {
        net group $Choice /domain > C:\Temp\RawOut.txt

        $UserID = Get-Content 'C:\Temp\RawOut.txt'
        $UserID[($UserID.IndexOf('-------------------------------------------------------------------------------') + 1) .. ($UserID.IndexOf('The command completed successfully.') -1)] > C:\Temp\RawIDs.txt

        #Start C:\Temp\RawIDs.txt

        Remove-Item C:\Temp\RawOut.txt

        $results = Get-Content -path C:\Temp\RawIDs.txt

            foreach($result in $results) { 
                ($result -replace '\s+',',') -split ',' | ? { $_ } >> 'C:\temp\users.txt'
                }

        Get-Content -path C:\Temp\users.txt | ForEach-Object {net user $_ /domain | findstr "Full Name"} >> C:\Temp\$Choice.txt

        Remove-Item C:\Temp\users.txt
        Remove-Item C:\Temp\RawIDs.txt
        Start C:\Temp\$Choice.txt
        }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM