如何在Powershell中创建只接受字符串和变量的函数?

如何在Powershell中创建只接受字符串和变量的函数?,powershell,Powershell,我想在Powershell中运行一个名为Get-OSArchitecture的函数,该函数告诉我当您给计算机一个域名时,它是32位系统还是64位系统。但是,它只接受诸如“SALES-DENNY”之类的字符串,而不接受带有诸如$string1之类的存储字符串的变量。我曾经尝试过一种叫做Out String的东西,但是这个函数在获取字符串时非常顽固,与变量无关 以下代码用于获取全局Get-OSArchitecture函数: function global:Get-OSArchitecture {

我想在Powershell中运行一个名为Get-OSArchitecture的函数,该函数告诉我当您给计算机一个域名时,它是32位系统还是64位系统。但是,它只接受诸如“SALES-DENNY”之类的字符串,而不接受带有诸如$string1之类的存储字符串的变量。我曾经尝试过一种叫做Out String的东西,但是这个函数在获取字符串时非常顽固,与变量无关

以下代码用于获取全局Get-OSArchitecture函数:

function global:Get-OSArchitecture { 

#Requires -Version 2.0 
[CmdletBinding()] 
 Param  
   ( 
    [Parameter(Mandatory=$false, 
               Position=1, 
               ValueFromPipeline=$true, 
               ValueFromPipelineByPropertyName=$true)] 
    [String[]]$ComputerName = $env:COMPUTERNAME       
   )#End Param  

Begin 
{ 
 Write-Verbose "Retrieving Computer Info . . ." 
} 
Process 
{ 
$ComputerName | foreach {  
$ErrorActionPreference = 0 
$Computer = $_ 
$Windir,$OSArchitecture,$OSVersion = Get-WmiObject -class Win32_OperatingSystem -ComputerName $_ |  
    foreach {$_.WindowsDirectory,$_.OSArchitecture,$_.Version} 
$SysDrive = ($Windir -split ":")[0] + "$" 
# $OSVersion[0] 
# $OSArchitecture is only suppored on OSVersion -ge 6 
# I was going to test for that, however now I just test if $OSArchitecture -eq $True 
Write-Verbose "Operating System version on $Computer is: $OSVersion" 
if ($OSArchitecture) 
    { 
        New-Object PSObject -Property @{  
        Hostname=$Computer 
        OSArchitecture=$OSArchitecture 
        SysDrive=$SysDrive 
        OSVersion=$OSVersion 
        WinDir=$WinDir 
        } 
    } 
else 
    { 
        # check the program files directory 
        write-verbose "System Drive on $Computer is: $SysDrive" 
        $x64 =  "\\$Computer\" + $SysDrive + "\Program Files (x86)" 
        if (test-path ("\\$Computer\" + $SysDrive)) 
            { 
                if (test-path $x64) 
                    { 
                        New-Object PSObject -Property @{  
                        Hostname=$Computer 
                        OSArchitecture="64-bit" 
                        SysDrive=$SysDrive 
                        OSVersion=$OSVersion 
                        WinDir=$WinDir 
                        } 
                    } 
                elseif (!(test-path $x64)) 
                    { 
                        New-Object PSObject -Property @{  
                        Hostname=$Computer 
                        OSArchitecture="32-bit" 
                        SysDrive=$SysDrive 
                        OSVersion=$OSVersion 
                        WinDir=$WinDir 
                        } 
                    } 
            } 
        else {"Something wrong determining the System Drive"}  
    } 
} | select Hostname,OSArchitecture,SysDrive,WinDir,OSVersion 

}#Process             
End             
{    

}#End  


}#Get-OSArchitecture 
我的问题从下面开始

$string1 = "SALES-DENNY"

Get-OSArchitecture $string1
上述方法失败了。 下面的方法很有效

Get-OSArchitecture "SALES-DENNY"

我希望函数能给出名为“SALES-DENNY”的计算机的正确体系结构,但如果我不将其作为字符串输入,我总是得到一个空白结果。

尽管将计算机名作为硬编码字符串或变量中的名称或IP输入并不重要,我相信您可以通过不测试
程序文件(x86)
目录来改进功能。
相反,您可以依赖另外两个WMI函数来获取操作系统的“比特度”:

function Get-OSArchitecture {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )
    process {
        foreach ($computer in $ComputerName) {
            Write-Verbose "Retrieving info for computer '$computer'"

            $info = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer | 
                    Select-Object @{Name = 'HostName'; Expression = { $_.PSComputerName}},
                                  OSArchitecture,
                                  @{Name = 'SysDrive'; Expression = { '{0}$' -f ($_.SystemDrive).Substring(0,1) }},
                                  @{Name = 'WinDir'; Expression = { $_.WindowsDirectory}},
                                  @{Name = 'OSVersion'; Expression = { $_.Version }}

            if ($info.OSArchitecture) {
                $info.OSArchitecture = '{0}-bit' -f ($info.OSArchitecture -replace '\D+','')
            }
            else {
                $info.OSArchitecture = '{0}-bit' -f (Get-WmiObject -Class Win32_Processor -ComputerName $computer).AddressWidth
                # or do:
                # $info.OSArchitecture = '{0}-bit' -f (((Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer).SystemType -replace '\D+', '') -replace '86', '32')
            }
            # emit info
            $info
        }
    }
}

希望这有帮助

传递字符串文字和包含字符串的变量之间应该没有区别。您的问题中没有足够的信息来诊断问题。请考虑提供一个。如果你说它失败了,它是怎么失败的?你有什么错误?另外,请确保你的代码样本足够小,它仍然可以重现这个问题。奇怪的是,它对我有效。我将上面的代码保存在get-osarchitecture.ps1中,然后输入
\获取osarchitecture
来定义函数,然后它就开始工作了。作为一名新手,您可能不知道这一点,但习惯于单击✓ 在左边。这将帮助其他有类似问题的人更容易找到它。不再有回应了吗?