Connect-MsolService - 提供凭证而不在脚本中暴露密码

Connect-MsolService - providing credentials without exposing password in script

我希望运行将以下 PowerShell 脚本作为计划任务从 Azure AD 中提取配置日志。但是,我不想嵌入密码。我明白这不是 PowerShell 或 Microsoft Online 特有的问题。我可以使用什么技术不将密码存储为明文?谢谢

剧本致谢:Pawel Janowicz

    $AzureUsername   = 'log.reader@tenant.net'
    $Password        = "xxxxxx"
    $SecureString    = ConvertTo-SecureString -AsPlainText $Password -Force
    $SecuredCreds    = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AzureUsername,$SecureString
    $OutputCSV       = "$Env:USERPROFILE\desktop\DirSyncProvisioningErrors_$(Get-Date -Format "yyyyMMdd").csv"
 
 
    ###### Connecting ############################################################################################################   
    Try{
        [void] (Connect-MsolService -Credential $SecuredCreds)
     
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $SecuredCreds -Authentication Basic -AllowRedirection
        [void] (Import-PSSession $Session -DisableNameChecking) 
    }
    Catch{
        $_.Exception.Message
        Read-Host 'Press enter to close the window'
        Remove-PSSession $Session
        Exit
    } 
 
 
    ###### Getting errors ########################################################################################################
    If(Get-MsolHasObjectsWithDirSyncProvisioningErrors){
        Try{
            $Errors = Get-MsolDirSyncProvisioningError -All | select DisplayName,ObjectID,ObjectType,ProvisioningErrors
            $Results = Foreach ($i in $Errors){
                $AllErrors = $i.ProvisioningErrors
                $AllErrors | %{
                    $ErrorItem = $_
                    Get-AzureADObjectByObjectId -ObjectIds $i.objectid | Foreach{
   
                        New-Object PSObject -Property ([ordered]@{ 
   
                            'Displayname'        = $i.displayname
                            'ObjectType'         = $i.ObjectType
                            'Attribute'          = $ErrorItem.propertyname
                            'Conflicting value'  = $ErrorItem.propertyvalue
                        })
                    } 
                }
            }
        }
        Catch{
            $_.Exception.Message
            Read-Host 'Press enter to close the window'
            Remove-PSSession $Session
            Exit
        }
    }
 
 
    ###### Results ###############################################################################################################
    If($Results){
        $Results | Format-Table -AutoSize
         
        #Exporting CSV
        $Results | Export-CSV $OutputCSV -NoTypeInformation -Force
    }
 
    Remove-PSSession $Session

感谢 Theo 提供您的建议作为评论。将其作为帮助其他社区成员的答案。

我已执行命令并显示输入密码,而不是在脚本本身中手动提供。

$SecuredCreds = Get-Credential -UserName 'log.reader@tenant.net' -Message "Please enter credentials"
$OutputCSV       = "$Env:USERPROFILE\desktop\DirSyncProvisioningErrors_$(Get-Date -Format "yyyyMMdd").csv"


###### Connecting ############################################################################################################   
Try{
    [void] (Connect-MsolService -Credential $SecuredCreds)
 
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $SecuredCreds -Authentication Basic -AllowRedirection
    [void] (Import-PSSession $Session -DisableNameChecking) 
}
Catch{
    $_.Exception.Message
    Read-Host 'Press enter to close the window'
    Remove-PSSession $Session
    Exit
} 


###### Getting errors ########################################################################################################
If(Get-MsolHasObjectsWithDirSyncProvisioningErrors){
    Try{
        $Errors = Get-MsolDirSyncProvisioningError -All | select DisplayName,ObjectID,ObjectType,ProvisioningErrors
        $Results = Foreach ($i in $Errors){
            $AllErrors = $i.ProvisioningErrors
            $AllErrors | %{
                $ErrorItem = $_
                Get-AzureADObjectByObjectId -ObjectIds $i.objectid | Foreach{

                    New-Object PSObject -Property ([ordered]@{ 

                        'Displayname'        = $i.displayname
                        'ObjectType'         = $i.ObjectType
                        'Attribute'          = $ErrorItem.propertyname
                        'Conflicting value'  = $ErrorItem.propertyvalue
                    })
                } 
            }
        }
    }
    Catch{
        $_.Exception.Message
        Read-Host 'Press enter to close the window'
        Remove-PSSession $Session
        Exit
    }
}


###### Results ###############################################################################################################
If($Results){
    $Results | Format-Table -AutoSize
     
    #Exporting CSV
    $Results | Export-CSV $OutputCSV -NoTypeInformation -Force
}

Remove-PSSession $Session