powershell 电源外壳

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 电源外壳相关的知识,希望对你有一定的参考价值。

$logFile = "result.txt"

# List files to text file
#Get-ChildItem \\lmlans01\Batch\informatica -File -Name | Out-File \\lmlans01\Batch\informatica\$logFile
#Get-Process -Name Excel | Out-File \\lmlans01\Batch\UploadApp\alextemp\$logFile
#Get-process -Name Excel | Stop-Process -Force
#Get-Process -Name Excel  | Out-File \\lmlans01\Batch\UploadApp\alextemp\$logFile

#Get-Process -id 5528 | Stop-Process -Force

try
{
    # Create new credential for temp drive
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "THELINKMGT\lmlqlikse_srv", (ConvertTo-SecureString "Qlikse@201807" -AsPlainText -Force)

    # Create temp drive
    $PSDriveMsg = New-PSDrive -Name AlexDrive -PSProvider FileSystem -Root \\lmlans01\Batch\UploadApp\alextemp -Credential $credential

    # Reset output file
    'program start' | Out-File -FilePath AlexDrive:\$logFile
    # Reset error file
    '' | Out-File -FilePath AlexDrive:\error.txt

    # Copy Item to temp dir
    Copy-Item -Path ('AlexDrive:\AlexCopy.xlsx') -Destination $env:TEMP -Force | Out-File AlexDrive:\$logFile -Append

    # Excel related
    $excelFile = "$($env:TEMP)\AlexCopy.xlsx"

    $currentTime = Get-Date
    $excelApp = New-Object -ComObject Excel.Application

    $excelApp.Visible = $true
    
    # Get Excel PID to kill process
    $excelPID = Get-Process -Name EXCEL | Where-Object {$_.StartTime -ge $currentTime} | Select -ExpandProperty ID
    $excelPID | Out-File -FilePath AlexDrive:\$logFile -Append

    $excelApp.ScreenUpdating = $false
    $excelApp.DisplayAlerts = $false 
    $excelApp.Visible = $false
    $excelApp.AlertBeforeOverwriting = $false
    $excelApp.AutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityForceDisable

    $wbs = $excelApp.Workbooks
    $wb = $wbs.Open($excelFile)
    	
    $wss = $wb.Worksheets
    $ws = $wss.Item("Raw Data")
    #$ws.Activate()

    #'wb'| Out-File AlexDrive:\$logFile -Append
    $wb | Get-Member *refresh* | Out-File AlexDrive:\$logFile -Append
    #'conns'| Out-File AlexDrive:\$logFile -Append
    #$conns | Get-Member *refresh* | Out-File AlexDrive:\$logFile -Append

    $wb.RefreshAll()
    #$excelApp.CalculateFullRebuild()
    

	#$app = $excelApp.Application
    #$app.CalculateUntilAsyncQueriesDone()
    #$excelApp.CalculateUntilAsyncQueriesDone()

#Start-Sleep -Seconds 10

	#$wb.Save()
	$ws.SaveAs("$($env:TEMP)\AlexCopy.csv", 6)

    $wb.Close($false)

    Get-Content "$($env:TEMP)\AlexCopy.csv" | Out-File AlexDrive:\$logFile -Append

    Copy-Item -Path ("$($env:TEMP)\AlexCopy.csv") -Destination "AlexDrive:\AlexCopy.csv" -Force | Out-File AlexDrive:\$logFile -Append
    Copy-Item -Path ("$($env:TEMP)\AlexCopy.xlsx") -Destination "AlexDrive:\AlexCopyBack.xlsx" -Force | Out-File AlexDrive:\$logFile -Append
}
catch
{
    $formatstring = "{0} : {1}`n{2}`n" +
                    "    + CategoryInfo          : {3}`n" +
                    "    + FullyQualifiedErrorId : {4}`n" +
                    "    + Exception Message : {5}`n" 
    $fields = $_.InvocationInfo.MyCommand.Name,
              $_.ErrorDetails.Message,
              $_.InvocationInfo.PositionMessage,
              $_.CategoryInfo.ToString(),
              $_.FullyQualifiedErrorId,
              $_.Exception.Message

    $formatstring -f $fields | Out-File AlexDrive:\error.txt 
    $formatstring -f $fields
}
finally
{
    $originalErrorActionPreference = $ErrorActionPreference
    $ErrorActionPreference = "SilentlyContinue"

    $excelApp.Quit()
	
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ws) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wss) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wbs) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp) | Out-Null
	
    [System.GC]::Collect() | Out-Null
    [System.GC]::WaitForPendingFinalizers() | Out-Null

    Remove-Variable ws
    Remove-Variable wss
    Remove-Variable wb
    Remove-Variable wbs
    Remove-Variable excelApp

    Get-Process -Id $excelPID | Stop-Process -Force | Out-File AlexDrive:\$logFile -Append
    # Sleep wait for process killed
    Start-Sleep -Seconds 3

    $ErrorActionPreference = $originalErrorActionPreference
}

#remove drive
Remove-PSDrive AlexDrive

Get-Process -Name Excel | Out-File \\lmlans01\Batch\UploadApp\alextemp\$logFile -Append

以上是关于powershell 电源外壳的主要内容,如果未能解决你的问题,请参考以下文章

powershell 重新安装NuGet包。 #电源外壳

powershell 电源外壳

powershell 电源外壳

powershell 电源外壳

powershell Azure文件存储服务电源外壳设置

powershell 测试电源外壳中是否存在Azure帐户,如果不存在则添加它