函数通过powershell启用基本身份验证

函数通过powershell启用基本身份验证,powershell,Powershell,我已经创建了一个powershell脚本来启用基本身份验证,我需要它来允许winrm在运行一些较旧的powershell脚本时工作。 我现在需要做的是能够调用这个脚本作为一个函数,使用一个true或false参数。e、 g.禁用或启用基本身份验证。 如何将此代码包装到函数中,以便从其他powershell脚本调用它? 因此,如果我向该脚本发送命令,例如 basicauth($true)-它将按原样运行脚本 basicauth($false-将禁用基本身份验证 我可以为何时将true或false发

我已经创建了一个powershell脚本来启用基本身份验证,我需要它来允许winrm在运行一些较旧的powershell脚本时工作。 我现在需要做的是能够调用这个脚本作为一个函数,使用一个true或false参数。e、 g.禁用或启用基本身份验证。 如何将此代码包装到函数中,以便从其他powershell脚本调用它?
因此,如果我向该脚本发送命令,例如
basicauth($true)-它将按原样运行脚本
basicauth($false-将禁用基本身份验证
我可以为何时将true或false发送到此脚本创建备用if-else语句,但不确定如何将整个脚本包装到函数中。
对于powershell的新手状态,我深表歉意,我花了一段时间才让这个脚本按原样运行

param([switch]$Elevated)
# Get variables
$registryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client"
$key1 = "AllowDigest"
$key2 = "AllowUnencryptedTraffic"
$key3 = "AllowBasic"
$off = "00000000"
$on = "00000001"
# enables admin privileges
function Test-Admin {
  $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false)  {
    if ($elevated) 
    {
        'tried to elevate, did not work, aborting...'
    } 
    else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}

# checks if the registry path is available, before adding the registry key values
        If (!(Test-Path $registryPath))
        {
            New-Item -Path $registryPath -Force | out-Null
            New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
            New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
            New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
            #'registry key did not exist'
            exit
        }
        Else 
        {
            New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
            New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
            New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
            #'registry key exists'
            exit
        }
注意:现在我知道Else语句的值应该是:Set ItemProperty,尽管如果我将代码更改为Set ItemProperty,脚本将不再工作,唯一可行的方法是将其设置为:New ItemProperty。虽然没有实际意义,但它可以工作。
理想情况下,最好只更新当前的powershell脚本以使用现代身份验证,但有100个,因此对我来说不是一个切实可行的选择。

非常感谢您的帮助。

如果您将整个函数包装在If语句中,如:

param([switch]$Elevated)

if($elevated) {
    ...script code here
}
然后,您可以使用该参数调用函数,如“scriptname.ps1”-提升以执行scriptblock中的内容。而只调用不带-highed参数的“scriptname.ps1”将不起任何作用,因为您将点击if语句:

if ($elevated) {
并且提升不存在,这意味着scriptblock中没有执行任何内容

我看不出在你的例子中这样做的目的,因为如果你已经有了逻辑来决定是否传入true或false,为什么不直接使用这个逻辑来决定是否调用脚本呢?我的猜测是,你实际上并不是说你只想在用户被“提升”时执行整个脚本,而是检查如果可以在脚本中提升它们,然后执行其他操作

在这种情况下,你应该看看。你可以这样做:

# Get variables
$registryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client"
$key1 = "AllowDigest"
$key2 = "AllowUnencryptedTraffic"
$key3 = "AllowBasic"
$off = "00000000"
$on = "00000001"
# enables admin privileges
function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    return $isAdmin
}

function Do-TheRestOfTheThings {
    [CmdletBinding()]
    param()
    Get-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    If (!(Test-Path $registryPath)) {
        New-Item -Path $registryPath -Force | out-Null
        New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
        #'registry key did not exist'
        exit
    }
    Else {
        New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
        #'registry key exists'
    }
}

if(Test-Admin) {
    Do-TheRestOfTheThings
}
else {
    'tried to elevate, did not work, aborting...'
}

如果将整个函数包装在If语句中,如:

param([switch]$Elevated)

if($elevated) {
    ...script code here
}
然后,您可以使用该参数调用函数,如“scriptname.ps1”-提升以执行scriptblock中的内容。而只调用不带-highed参数的“scriptname.ps1”将不起任何作用,因为您将点击if语句:

if ($elevated) {
并且提升不存在,这意味着scriptblock中没有执行任何内容

我看不出在你的例子中这样做的目的,因为如果你已经有了逻辑来决定是否传入true或false,为什么不直接使用这个逻辑来决定是否调用脚本呢?我的猜测是,你实际上并不是说你只想在用户被“提升”时执行整个脚本,而是检查如果可以在脚本中提升它们,然后执行其他操作

在这种情况下,你应该看看。你可以这样做:

# Get variables
$registryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client"
$key1 = "AllowDigest"
$key2 = "AllowUnencryptedTraffic"
$key3 = "AllowBasic"
$off = "00000000"
$on = "00000001"
# enables admin privileges
function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    return $isAdmin
}

function Do-TheRestOfTheThings {
    [CmdletBinding()]
    param()
    Get-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    If (!(Test-Path $registryPath)) {
        New-Item -Path $registryPath -Force | out-Null
        New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
        #'registry key did not exist'
        exit
    }
    Else {
        New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
        #'registry key exists'
    }
}

if(Test-Admin) {
    Do-TheRestOfTheThings
}
else {
    'tried to elevate, did not work, aborting...'
}

测试开关是否存在:if($highted.IsPresent)@RetiredGeek-我已经重新表述了我的初始问题。测试开关是否存在:if($highted.IsPresent)@RetiredGeek-我已经重新表述了我最初的问题。感谢您的提问,我将对其进行测试,并让您知道它是如何运行的。基本上,我有许多使用基本身份验证的powershell脚本,我已经“继承”了这些脚本。而不是更新每个脚本,我只想调用此脚本以启用基本身份验证,然后在脚本末尾再次禁用基本身份验证。因此,将此脚本称为:basicauth($true)以启用它,然后在脚本末尾basicauth($false)再次禁用它。感谢您的帮助,我将对其进行测试,并让您了解它的运行情况。基本上,我有许多使用基本身份验证的powershell脚本,我已“继承”了这些脚本。而不是更新每个脚本,我只想调用此脚本以启用基本身份验证,然后在脚本末尾再次禁用基本身份验证。因此,将此脚本称为:basicauth($true)以启用它,然后在脚本末尾basicauth($false)以再次禁用它。