您好,登錄后才能下訂單哦!
本文以azure為例,為大家分析用powershell腳本查看用戶的操作記錄的方法。閱讀完整文相信大家對powershell腳本的使用方法有了一定的認識。
下邊來看下代碼的內容,其實是很簡單的
param ( [parameter(Mandatory = $false)] [Int]$MaxRecords = 100000, [parameter(Mandatory = $true)] [string]$User ) function Write-DateTimeMessage { param ( [parameter(Mandatory = $false)] [switch]$Warning, [parameter(Mandatory = $true)] [string]$Message, [parameter(Mandatory = $false)] [string]$ForegroundColor ) if ($Warning) { Write-Warning ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) } else { if ($ForegroundColor) { Write-Host ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) -ForegroundColor $ForegroundColor } else { Write-Host ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) } } } [pscustomobject[]]$UserObjects = $null $Subscriptions = Get-AzureRmSubscription foreach ($subscription in $Subscriptions) { " " "Querying Subscription:" $SubscriptionID = $Subscription.Id $SubscriptionName = $Subscription.Name Select-AzureRmSubscription -SubscriptionId $SubscriptionID -InformationAction SilentlyContinue Write-DateTimeMessage -Message "Retrieving logs, please wait..." $logs = Get-AzureRmLog -ResourceProvider Microsoft.Compute -StartTime (Get-Date).AddDays(-90) -Maxrecord $MaxRecords foreach ($log in $logs) { if ($log.caller -eq $User) { $UserObject = New-Object -TypeName psobject $UserObject | Add-Member -MemberType NoteProperty -Name SubscriptionName -Value $SubscriptionName $UserObject | Add-Member -MemberType NoteProperty -Name SubscriptionID -Value $SubscriptionID $UserObject | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $log.ResourceGroupName $UserObject | Add-Member -MemberType NoteProperty -Name Caller -Value $log.caller $UserObject | Add-Member -MemberType NoteProperty -Name Operation -Value $log.OperationName.Value $UserObject | Add-Member -MemberType NoteProperty -Name ResourceId -Value $log.ResourceId $UserObject | Add-Member -MemberType NoteProperty -Name Time -Value $log.EventTimestamp $UserObjects += $UserObject } } } $OutputPath = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath ("AzureUserAction-" + $(Get-Date -Format "yyyyMMdd-HHmmss") + ".csv") if ($null -ne $UserObjects) { $UserObjects | Export-Csv -NoTypeInformation -LiteralPath $OutputPath Write-DateTimeMessage -Message "Please check $OutputPath" -Warning } else { Write-DateTimeMessage "Didn't get information, please check" -warning }
我們來嘗試著運行一下腳本Get-AzureUserActionLog.ps1 -User "xxx@xxx.partner.onmschina.cn", -User的作用是我們可以根據這個參數篩選出來特定的用戶
腳本執行完成后,可以在桌面上看到一個csv文件,里邊會記錄查詢出來log
提醒一點,因為Azure后臺的限制,這只能查詢到最近90天之內的log。
看完這篇文章,你們學會用powershell腳本查看用戶的操作記錄了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。