Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

batch processing - Write PowerShell script inside a .bat file

I can't create a PowerShell file, and I can only use an existing .bat file.

Here is my PowerShell script, which checks if there is a hdd hard drive and return a boolean.

$partitions = Get-CimInstance Win32_DiskPartition
$physDisc = get-physicaldisk
$arr = @()
foreach ($partition in $partitions){
    $cims = Get-CimInstance -Query "ASSOCIATORS OF `
                          {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} `
                          WHERE AssocClass=Win32_LogicalDiskToPartition"
    $regex = $partition.name -match "(d+)"
    $physDiscNr = $matches[0]
    foreach ($cim in $cims){
        if($cim.deviceID -ne "C:" -and $cim.deviceID -ne "D:") {
            $arr += [PSCustomObject]@{
                MediaType = $($physDisc | ? {$_.DeviceID -eq $physDiscNr} | select -expand MediaType)
            }
        }
    }
}

($arr).MediaType -contains "hdd"

I did not find how to write correctly my script inside the Batch instruction powershell.exe

@ECHO off
powershell.exe "My powershell script here">temp.txt
set /p areFullSSD=<temp.txt
if NOT %areFullSSD%==False goto :noSSD
ECHO areFullSSD
:noSSD
ECHO test
PAUSE

Thank you for the help.

question from:https://stackoverflow.com/questions/65915349/write-powershell-script-inside-a-bat-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the Microsoft documentation,

For inline scripts,

To execute an inline script block defined inside a string, the call operator & can be used:

pwsh -Command "& {Get-WinEvent -LogName security}"

The documentation for this with the parameters is available at the following webpage,

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...