1

我对 Powershell 很陌生,实际上是从今天开始的,我需要一些帮助来获取显示输出的命令。该命令在从 powershell 运行时有效,但是当我尝试从我粘贴的脚本运行时,它似乎失败了。我什至将变量字段更改为我的用户名,但它仍然失败。

Get-ADUser jdoe -Properties Description

就像我说的那样,它在 powershell 中运行时有效,但在脚本中运行时无效。

Clear-Host

$script:ChosenFunction=Get-function  #Get-Choice
 

function Get-function
{
    Write-Host "1. IP (future addon)"
    write-host "2. UserInfo"
    write-host "3. CopyZ (Future addon)"
    write-host "4. Local Users (Future addon)"
    Write-Host "X. Exit"

    $Action=Read-Host "Choose an action:"
    Switch ($Action)
    {
        1 {$Choice="IP"}
        2 {$Choice="UserInfo"}
        3 {$Choice="CopyZ"}
        4 {$Choice="Local Users"}
        5 {$Choice="Exit"}
    }
    
    write-host "Function chosen is $Choice"

    if ($Action -eq 2)
    {
        write-host "if is working"
        $script:ChosenFunction=userInfo
    }

}

function userInfo
{
    cls
    $Name=Read-Host "Enter user name: "
    Write-Host "finding $Name"

    Get-ADUser $Name -Properties Description # <---- Here's my problem get user's AD information
}
4

2 回答 2

0

第一天印象非常深刻!通过阅读评论,我了解您已经知道为什么userInfo没有产生任何输出,这里是您可以简化代码并可能有助于学习新东西的方法。我添加了评论以帮助您了解思考过程。

function Show-Menu {
# Simple function to Show a Menu each time it's called
    Clear-Host
    Write-Host "1. IP (future addon)"
    write-host "2. UserInfo"
    write-host "3. CopyZ (Future addon)"
    write-host "4. Local Users (Future addon)"
    Write-Host "X. Exit"
}

function Get-UserInfo {
# Function to get a user from AD
param(
    [parameter(Mandatory)]
    [string]$User
)
    try
    {
        Get-ADUser $Name -Properties Description
    }
    catch
    {
        $PSCmdlet.WriteError($_)
    }
}

function WaitForInput {
# Stops the loop until a Key is pressed
    $null = [console]::ReadKey()
}

do  # Start a loop
{
    
    Show-Menu # Show the Menu
    $Action = Read-Host "Choose an Action" # Get input from user
    Switch ($Action) # Switch the input to see which option was chosen
    {
        1 { "IP" }
        2 {
            # If option 2 was chosen
            $Name = Read-Host "Enter User Name" # Ask for User to Find
            Write-Host "Finding $Name" # Display the user to Find
            Get-UserInfo -User $Name # Call the Function passing the user as Argument
            WaitForInput # Wait here until a Key is Pressed
        }
        3 { "CopyZ" }
        4 { "Local Users" }
    }
} until($Action -eq 'X') # Loop until X was chosen
于 2022-01-20T00:10:25.973 回答
0

关于这个脚本有太多要说的了,所以让我们专注于你的主要问题......

首先,您需要在脚本的主要部分之前声明函数。 如果在脚本中声明(写入)函数之前调用它,它将在调用时失败。

第二件事,正如评论中提到的,当您将 Get-ADUser 的结果分配给变量 $Script:ChoosenFunction ...并且您执行两次时,您不能期望屏幕上出现输出。请参阅下面的评论:

Clear-Host

function Get-function
{
    Write-Host "1. IP (future addon)"
    write-host "2. UserInfo"
    write-host "3. CopyZ (Future addon)"
    write-host "4. Local Users (Future addon)"
    Write-Host "X. Exit"

    $Action=Read-Host "Choose an action"
    Switch ($Action)
    {
        1 {$Choice="IP"}
        2 {$Choice="UserInfo"}
        3 {$Choice="CopyZ"}
        4 {$Choice="Local Users"}
        5 {$Choice="Exit"}
    }
    
    write-host "Function chosen is $Choice"

    if ($Action -eq 2)
    {
        write-host "if is working"
        $script:ChosenFunction=userInfo # Assign the value to $script:ChosenFunction but function doesn't return any value
        return $script:ChosenFunction # return value to caller (that's missing).
    }
}

function userInfo
{
    cls
    $Name=Read-Host "Enter user name"
    Write-Host "finding $Name"

    Get-ADUser $Name -Properties Description # <---- Here's my problem get user's AD information
}

$script:ChosenFunction=Get-function  #Why reassigning the value to $script:ChosenFunction also here?
# Display the content of $script:ChosenFunction
$script:ChosenFunction

# Or simply call the Get-Function as the value of $script:ChosenFunction is already set within the function itself
Get-Function

下面是一个示例,如果相同的脚本代码更少:

Clear-Host

function Get-function {
    Write-Host "1. IP (future addon)" ;
    write-host "2. UserInfo" ;
    write-host "3. CopyZ (Future addon)" ;
    write-host "4. Local Users (Future addon)" ;
    Write-Host "X. Exit" ;

    Switch (Read-Host "Choose an action") {
        1 {$Choice="IP"}
        2 {$Choice="UserInfo";userInfo}
        3 {$Choice="CopyZ"}
        4 {$Choice="Local Users"}
        5 {$Choice="Exit"}
    }
    
    Write-Host "Function choosen is: $Choice" ;
}

function userInfo {
    Clear-Host ;
    $Name = Read-Host "Enter user name" ;
    Write-Host "finding $Name" ;
    Get-ADUser $Name -Properties Description ;
}

$script:ChosenFunction = Get-function ;
# Display Result
$script:ChosenFunction ;
于 2022-01-19T23:28:39.320 回答