powershell 此PowerShell脚本是从SCCM DB获取SCEP日志并将其导出为CSV文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 此PowerShell脚本是从SCCM DB获取SCEP日志并将其导出为CSV文件相关的知识,希望对你有一定的参考价值。

<#
*********************************************************************************************************
*                                                                                                       *
*** This PowerShell script is Get the SCEP Logs from SCCM DB and export them to a CSV file            ***
*                                                                                                       *
*********************************************************************************************************
* Created by Octavian Cordos, 2017-03-28   | Requirements: PowerShell 3.0, SQL PS commandlets           *
* ======================================================================================================*
* Modified by                   | Date       | Version  | Comments                                      *
*_______________________________________________________________________________________________________*
* Octavian Cordos               | 2017-03-28 | v1.0     | First version                                 *
* Ioan Popovici                 | 2017-03-28 | v1.1     | Minor modifications, cleanup                  *
* Ioan Popovici                 | 2017-09-11 | v1.2     | Fix $ScriptName variable                      *
*-------------------------------------------------------------------------------------------------------*
* Execute with: C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoProfile -File              *
* Get-SCEPDetectionLogs.ps1 -CMSQLServer "SCCM_SQL_Server_Name" -CMDatabase "CM_Database_Name"          *
*********************************************************************************************************

    .SYNOPSIS
        This PowerShell script is Get the SCEP Logs from SCCM DB and export them to a CSV file
    .DESCRIPTION
        This PowerShell script is Get the SCEP Logs from SCCM Database and export them to a CSV file.
#>

##*=============================================
##* VARIABLE DECLARATION
##*=============================================
#region VariableDeclaration

    ## External Script Variables
    Param (
        [Parameter(Mandatory=$True,Position=0)]
        [Alias('CMSQL')]
        [string]$CMSQLServer,
        [Parameter(Mandatory=$True,Position=1)]
        [Alias('CMDB')]
        [string]$CMDatabase
    )

    ## Get script path and name
    [String]$ScriptPath = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)
    [String]$ScriptName = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Definition)

    ## CSV file initialization
    #  Set the CSV Data file name
    [String]$csvDataFileName = $ScriptName
    [String]$csvDataFileNameWithExtension = $csvDataFileName+'.csv'

    #  Assemble CSV Data file path
    [String]$csvDataFilePath = (Join-Path -Path $ScriptPath -ChildPath $csvDataFileName)+'.csv'

#endregion
##*=============================================
##* END VARIABLE DECLARATION
##*=============================================

##*=============================================
##* SCRIPT BODY
##*=============================================
#region ScriptBody

    ## Check if Get-SCEPDetectionLogs.cvs exists
    If (-not (Test-Path $csvDataFilePath)) {
        New-Item -Path $ScriptPath -Name $csvDataFileNameWithExtension -Type 'File'
    }

    ## Get Last Write Time Stamp
    $LastWriteTime = Import-CSV $csvDataFilePath | Select-Object -ExpandProperty Timestamp -Last 1

    ## Get Logs from Database using the CSV Last Write Time Stamp in order to pick up where the last Get-SCCMDetectionLog cycle ended
    $SCEPLogData = Invoke-SQLcmd -Query "SELECT * FROM $($CMDatabase).dbo.v_AM_NormalizedDetectionHistory SCEP WHERE SCEP.TimeStamp > `'$LastWriteTime`' ORDER BY SCEP.TimeStamp ASC" -ServerInstance $CMSQLServer

    ## Check if we have something to write to the CSV file
    If (-not ($SCEPLogData)) {

        # Overwrite the CSV file with gathered SCEP Log Data if the size gets over 2500 KB
        If ((Get-Item $csvDataFilePath).Length -gt 2500KB) {
            $SCEPLogData | Export-CSV $csvDataFilePath -NoTypeInformation -Force -Encoding 'UTF8' -ErrorAction 'Continue'
        }

        # Append gathered SCEP Log Data to the CSV file
        Else {
            $SCEPLogData | Export-CSV $csvDataFilePath -Append -NoClobber -Force -Encoding 'UTF8' -ErrorAction 'Continue'
        }
    }

#endregion
##*=============================================
##* END SCRIPT BODY
##*=============================================

以上是关于powershell 此PowerShell脚本是从SCCM DB获取SCEP日志并将其导出为CSV文件的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell 错误“在此系统上禁用脚本执行”。

powershell 此powershell脚本使用字段名称获取所有值是sharepoint列表中的字段

powershell 此PowerShell脚本从子站点组等于值的所有子站点中删除组。如果要删除gro,请使用此选项

powershell 此PowerShell脚本是从SCCM DB获取SCEP日志并将其导出为CSV文件

powershell 此powershell脚本生成新证书,从IISExpress ssl端口删除旧证书分配并添加新证书

运行powershell 脚本 在此系统上禁止运行脚本