相关疑难解决方法(0)

Get-Content 真正输出什么,一个字符串或几个属性?

什么Get-Content真正输出,一个字符串或一个具有多个属性的对象?如果它是一个对象,哪个属性包含字符串,我可以修改该属性吗?该文件只有一行,所以我不必处理对象数组。

PS C:\Users\me> Get-Content input.txt
111

PS C:\Users\me> Get-Content input.txt | 选择对象 *

PSPath : C:\Users\me\input.txt
PSParentPath : C:\Users\me
PSChildName : 输入
驱动器:C
PSProvider : Microsoft.PowerShell.Core\FileSystem
读取计数:1
长度 : 3

PS C:\Users\me> (Get-Content input.txt).GetType()

IsPublic IsSerial 名称 BaseType
——————————————————
真真字符串 System.Object

-replace 擦除除长度以外的所有额外属性:

PS C:\Users\me> (Get-Content input.txt) -replace 111, 222 | 选择对象 *

长度
------
     3

string powershell

4
推荐指数
2
解决办法
2579
查看次数

PowerShell 5.1 为什么这两个函数返回不同的类型

function Main {
    $result1 = DoWork1
    $result1.GetType()

    $result2 = DoWork2
    $result2.GetType()
}

function DoWork1 {
    $result1 = Invoke-Sqlcmd -Query "select top 1 * from customer" -ServerInstance "(localdb)\MSSQLLocalDB" -Database "Database1" -OutputAs DataTables
    #assign to variable then return
    return $result1
}

function DoWork2 {
    #return results without assigning to variable
    return Invoke-Sqlcmd -Query "select top 1 * from customer" -ServerInstance "(localdb)\MSSQLLocalDB" -Database "Database1" -OutputAs DataTables
}

Main
Run Code Online (Sandbox Code Playgroud)

这是意外的输出:

IsPublic IsSerial Name                                     BaseType                                                                                    
-------- -------- ----                                     --------                                                                                    
True     False    DataRow                                  System.Object                                                                               
True     True …
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
151
查看次数

Get-ChildItem.Length是错误的

我正在编写一个遍历目录的递归函数,并复制其中的每个文件和文件夹.我在函数中的第一个检查是查看传入的路径是否有子节点.为了找到这个,我使用以下方法:

[array]$arrExclude = @("Extras")
Function USBCopy
{
Param ([string]$strPath, [string]$strDestinationPath)
    try
    {
        $pathChildren = Get-ChildItem -Path $strPath
        if($pathChildren.Length -gt 0)
        {
            foreach($child in $pathChildren)
            {
                if($arrExclude -notcontains $child)
                {
                    $strPathChild = "$strPath\$child"
                    $strDestinationPathChild = "$strDestinationPath\$child" 
                    Copy-Item $strPathChild -Destination $strDestinationPathChild
                    USBCopy $strPathChild $strDestinationPathChild  
                }   
            }
        }              
    }
    catch
    {
        Write-Error ("Error running USBCopy: " + $Error[0].Exception.Message)
    }    
}
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,我的函数可以工作,但我的代码会说当一个目录实际上有一个文件时它是空的.当我调试我的函数时,变量会说该文件夹有子项但变量的长度为0.任何人都知道如何解决这个问题?

arrays powershell member-enumeration

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

带有格式运算符的 Powershell 颜色格式

我在脚本中使用格式运算符,示例如下:

$current = 1
$total   = 1250
$userCN  = "contoso.com/CONTOSO/Users/Adam Smith"

"{0, -35}: {1}" -f "SUCCESS: Updated user $current/$total", $userCN
Run Code Online (Sandbox Code Playgroud)

这 expectedly 显示以下输出:

SUCCESS: Updated user 1/1250 : contoso.com/CONTOSO/Users/Adam Smith

格式运算符用于将目标输出文本保持在原位,当前/总运行数字在 1-99999 之间变化。如果没有格式运算符,我可以像这样突出显示成功行:

Write-Host -BackgroundColor Black -ForegroundColor Green "SUCCESS: Updated user $current/$total: $userCN"

但问题是如何将高亮颜色与格式运算符结合使用?只有-f参数,它不允许颜色参数,因为它Write-Host与最初的不同。

syntax powershell parameter-passing

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

PowerShell 空数组初始化在条件变量赋值中失败

在这篇关于如何在 PowerShell 中最好地创建(三元运算符)表达式的文章之后,我尝试将其转换:

if ($condition)
{ 
   $accessList = @() 
}
else
{
    $accessList = $accessControl.AllowedItems
}
Run Code Online (Sandbox Code Playgroud)

进入这个:

$accessList = if ($condition) { @() } else { $accessControl.AllowedItems }
Run Code Online (Sandbox Code Playgroud)

单行代码无法初始化空数组。结果$accessList没有任何数组属性/方法并$NULL -eq $accessList返回True。这个版本也不起作用:

$accessList = $(if ($condition) { @() } else { $accessControl.AllowedItems })
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我尝试初始化一个非空数组,单行代码就会起作用。它可能与变量评估和管道的工作原理有关,但在谷歌搜索了一下之后,我发现没有对这种特殊情况的详细解释。请帮助理解其背后的机制。

arrays powershell

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