0

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.

3
  • 2
    Why cannot you create a .ps1 file? If that's an permission issue, talk to the administrator / boss / teacher or the like and get permissions. That being said, look at encoded command Commented Jan 27, 2021 at 9:03
  • 1
    + for encoded command. Or place your script in a txt file and in your batch: powershell.exe -command "Get-Content 'path\to\script.txt' | powershell.exe ". Or take alook at 15-ways-to-bypass-the-powershell-execution-policy Commented Jan 27, 2021 at 10:01
  • Thank you for the help, -EncodedCommand is the solution and work fine ! Commented Jan 27, 2021 at 10:50

1 Answer 1

2

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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.