3

在删除页面文件并重新启动后,我试图在启动时将硬盘重新格式化为 64K,但Format-Drivecmdlet 没有运行。

我还尝试将触发器更改为在用户登录时运行。

关于我所忽略的任何建议?

workflow Resume_Workflow {
    # get all the drives not formatted 64k
    $driveLetter = ((Get-WmiObject -Class Win32_Volume |
        Where-Object {($_.SystemVolume -eq  "") -and ($_.BootVolume -ne "True") -and (($_.BlockSize/1024) -ne "64")} |
        Select DriveLetter, @{Label="Allocation Unit";Expression={$_.BlockSize/1024} }).DriveLetter)

    #find the drives that are not formatted 64k and also a page file drive
    $driveLetter_page = ((Get-WmiObject -Class Win32_Volume |
        Where-Object {($_.SystemVolume -eq  "") -and ($_.BootVolume -ne "True") -and (($_.BlockSize/1024) -ne "64") -and ($_.PageFilePresent -eq "True")} |
        Select DriveLetter, @{Label="Allocation Unit";Expression={$_.BlockSize/1024} }).DriveLetter)

    foreach ($i in $driveLetter) {
        $dp = $i.Substring(0,1).ToLower() 

        if ($i -in $driveLetter_page) {
            InlineScript {
                try {
                    $p = gwmi Win32_PagefileSetting |
                         Where-Object {$_.Name -contains  $using:dp + ':\pagefile.sys' };
                    "$using:dp page" | Out-File "p:\testing1.txt";
                    $p.Delete()
                } catch {$null}
            }
            Restart-Computer -Wait
            Format-Volume -DriveLetter $dp -AllocationUnitSize 65536 -FileSystem NTFS -Force -Confirm:$false
            #InlineScript {Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{ Name = $using:dp + ':\pagefile.sys'} }
            Restart-Computer -Wait
        } else {
            Format-Volume -DriveLetter $dp -AllocationUnitSize 65536 -FileSystem NTFS -Force -Confirm:$false
        }
    }
    #Unregister-ScheduledJob -Name Resume_Workflow_Job -Force
}
# Create the scheduled job properties
$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
$secpasswd = ConvertTo-SecureString "Aa123456!" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("WELCOME\Administrator", $secpasswd)
$AtStartup = New-JobTrigger -AtStartup
# Register the scheduled job
Register-ScheduledJob -Name Resume_Workflow_Job -Trigger $AtStartup -ScriptBlock ({
    [System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true;
    Import-Module PSWorkflow;
    Resume-Job -Name new_resume_workflow_job -Wait
}) -ScheduledJobOption $options
# Execute the workflow as a new job
Resume_Workflow -AsJob -JobName new_resume_workflow_job
#Unregister-ScheduledJob -Name Resume_Workflow_Job -force

一个简单的测试用例:

workflow Resume_Workflow {
    Restart-Computer -Wait
    $dp = "d"

    Start-Sleep -s 30

    Format-Volume -DriveLetter $dp -AllocationUnitSize 65536 -FileSystem NTFS 
}
$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
$secpasswd = ConvertTo-SecureString "Aa123456!" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("WELCOME\Administrator", $secpasswd)
$AtStartup = New-JobTrigger -AtLogon -User $env:UserName -RandomDelay 00:00:30
# Register the scheduled job
Register-ScheduledJob -Name Resume_Workflow_Job -Trigger $AtStartup -ScriptBlock ({[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true; Import-Module PSWorkflow; Resume-Job -Name new_resume_workflow_job -Wait}) -ScheduledJobOption $options
# Execute the workflow as a new job
Resume_Workflow -AsJob -JobName new_resume_workflow_job
#Unregister-ScheduledJob -Name Resume_Workflow_Job -Force
4

0 回答 0