相关疑难解决方法(0)

Powershell陷阱

您遇到的Powershell陷阱是什么?:-)

我的是:

# -----------------------------------
function foo()
{
    @("text")
}

# Expected 1, actually 4.
(foo).length

# -----------------------------------
if(@($null, $null))
{
    Write-Host "Expected to be here, and I am here."
}

if(@($null))
{
    Write-Host "Expected to be here, BUT NEVER EVER."
}

# -----------------------------------

function foo($a)
{
    # I thought this is right.
    #if($a -eq $null)
    #{
    #    throw "You can't pass $null as argument."
    #}

    # But actually it should be:
    if($null -eq $a)
    {
        throw "You can't pass $null …
Run Code Online (Sandbox Code Playgroud)

powershell

24
推荐指数
6
解决办法
4654
查看次数

PowerShell输出在功能之间交叉

我正在Windows 10的5.1版中编写PowerShell脚本,该脚本可获取有关本地系统(最终是其子网)的某些信息,并将其输出到文本文件中。首先,我将所有方面都集成到一个函数中。我在输出getUsersAndGroupsgetRunningProcesses函数时遇到输出问题,其中的输出getUsersAndGroups将注入到的输出中getRunningProcesses

这两个功能是:

    # Powershell script to get various properties and output to a text file

    Function getRunningProcesses()
    {
        # Running processes
        Write-Host "Running Processes:
    ------------ START PROCESS LIST ------------
        "
        Get-Process | Select-Object name,fileversion,productversion,company
        Write-Host "
    ------------- END PROCESS LIST -------------
    "
    }

    Function getUsersAndGroups()
    {
        # Get Users and Groups
        Write-Host "Users and Groups:"
        $adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
        $adsi.Children | where {$_.SchemaClassName -eq 'user'} | Foreach-Object {
            $groups = $_.Groups() | …
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
1
解决办法
145
查看次数

标签 统计

powershell ×2