尝试将Powershell脚本转换为VBscript

时间:2019-10-28 12:49:12

标签: powershell vbscript conditional-statements

在Lee_Dailey和js2010的帮助下弄清楚了!谢谢伙计们!

编辑 我试图弄清楚为什么通过SCCM将其部署到计算机集合时会得到误报。我发现设备实际上不合规。 SCCM操作员要求我提供脚本的VB版本。我正在排除潜在的原因。

我正在尝试将PowerShell脚本的功能转换为VBScript。该脚本检查设备上的AzureAdPrt令牌,但挑战在于该命令的输出,因为该命令是在表中格式化的,我只想检查一个特定值。

我试图将这项技术复制到VBScript,但是我对此没有经验。

Powershell代码:

    $dsregcmd = dsregcmd /status
    $PRTDetails = New-Object -TypeName PSObject
    $dsregcmd | Select-String -Pattern " *[A-z]+ : [A-z]+ *" | ForEach-Object {
              Add-Member -InputObject $PRTDetails -MemberType NoteProperty -Name (([String]$_).Trim() -split " : ")[0] -Value (([String]$_).Trim() -split " : ")[1]
         }
$PRT_Status = ($PRTDetails).AzureAdPrt
IF($PRT_Status -eq 'YES'){
    return $True
}ELSE{
    return $False
}

VB代码:

Set oShell = CreateObject ("WScript.Shell")
oShell.run "cmd /c dsregcmd /status"
IF oShell = "AzureAdPrt : YES"
  echo $true
ELSE
  echo $false

我要寻找的是如果AzureAdPrt等于YES的VBScript返回$ true,否则返回$ False。

1 个答案:

答案 0 :(得分:0)

您可以使用vbscript尝试类似的操作:Demo Here

Option Explicit
Dim oShell,Data,Extracted_Data
Set oShell = CreateObject ("WScript.Shell")
oShell.run "cmd /c dsregcmd /status > .\AzureLog.txt",0,True
Data = ReadFile(".\AzureLog.txt")
wscript.echo Data
Extracted_Data = Extract(Data)

IF Extracted_Data = "AzureAdPrt : YES" Then
    wscript.echo Extracted_Data & vbCrlf & "True"
Else
    wscript.echo Extracted_Data & vbCrlf & "False"
End If

'---------------------------------------------------------------
Function ReadFile(path)
    Const ForReading = 1
    Dim objFSO,objFile
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(path,ForReading)
    ReadFile = objFile.ReadAll
    objFile.Close
End Function
'---------------------------------------------------------------
Function Extract(Data)
    Dim RegEx,Match,Matches,M
    Set RegEx = New RegExp
    With RegEx
        .Pattern = "\bAzureAdPrt : \w+"
        .IgnoreCase = True
        .Global = True
    End With
    Set Matches = RegEx.Execute(Data)
    'Iterate through the Matches collection.
    For Each Match in Matches
        'We only want the first match.
        M = Match.Value
        Exit For
    Next
    Extract = M
    'Clean up
    Set Match = Nothing
    Set RegEx = Nothing
End Function
'---------------------------------------------------------------