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.

Awesome JSF
  • 85
  • 1
  • 8
  • 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](https://stackoverflow.com/a/47173505/503046) – vonPryz Jan 27 '21 at 09: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](https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/) – T-Me Jan 27 '21 at 10:01
  • Thank you for the help, -EncodedCommand is the solution and work fine ! – Awesome JSF Jan 27 '21 at 10:50

1 Answers1

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

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5