Convert PS to CMD

Page 9 of 24 FirstFirst ... 789101119 ... LastLast

  1. Posts : 3,275
    Win10
       #81

    This is one variation; though it requires additional code to verify (a) that the input value is valid & (b) to exit the script if the user presses the Cancel button etc. I am certain you'll thrash out a solution !

    This is using another variation of your method to start a Powershell script from a bat file without using an intermediate temp file (see the ref. inside the script) - maybe you could see if you can adapt it to your method.

    Set-Volume-Level.BAT
    Code:
    <# ::
    
    :: ref: https://www.dostips.com/forum/viewtopic.php?f=3&t=5526&start=15
    :: Do not alter the Re-Director at the top and the next one after the EOF command 
    
    :: RUN THIS BAT AS ADMIN using next line
    if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
    
    :: minimize this bat file
    if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
    
    setlocal
    powershell -noprofile -ExecutionPolicy ByPass "[ScriptBlock]::Create((${%~f0} | out-string)) | Invoke-Expression" & Exit
    goto :EOF
    
    #>
    
    
    ###### all ps now ######
    
    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    
    $title = 'Set the Volume Level between:  0-100'
    $Level  =  
    $text = [Microsoft.VisualBasic.Interaction]::InputBox($Level, $title)
    
    
    Function Set-SpeakerVolume{
    
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    
    Exit
    Hope it works.
      My Computers


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #82

    Hello @das10,

    das10 said:
    Hope it works.
    It does indeed work as is, thanks.

    Yes, I will have a play around to incorporate it into the rest of the Script.

    I must admit that I did think using the Read-Host -Prompt to set the Variable would have worked in place of the hard-coded 30, but I just couldn't get it to work.
      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #83

    I have been trying this approach [ it's close ] without much success . . .

    Code:
    
    @echo off
    set "Source=%~0"
    set "Function=%Temp%\Speaker_Volume.ps1"
    PowerShell "[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic'); $title='Set the Volume Level between:  0-100'; $Level=''; $text=[Microsoft.VisualBasic.Interaction]::InputBox($Level, $title)"
    del %Function% 2>nul & MORE /E +9 "%Source%" > %Function%
    PowerShell "Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File ""%Function%""' -Verb RunAs -WindowStyle Hidden"
    
    pause
    
    Function Set-SpeakerVolume{
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    Exit
    
    
    

    It just Mutes the speaker after the input of the volume input.

    I have tried moving the position of the new line of code etc, and the volume meter comes up, but that's it !

    The screen flashes so I tried using -WindowStyle Hidden but that didn't work.

    I will have another look tomorrow.
    Last edited by Paul Black; 22 Jul 2022 at 02:19.
      My Computer


  4. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #84

    I have also tried this . . .

    Code:
    
    @echo off
    set "Source=%~0"
    set "Function=%Temp%\Speaker_Volume.ps1"
    set Level=0
    set /p "Level=Enter Level: "
    PowerShell "Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File ""%Function%""' -Verb RunAs -WindowStyle Hidden"
    del %Function% 2>nul & MORE /E +10 "%Source%" > %Function%
    
    pause
    
    Function Set-SpeakerVolume{
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent,
      [Int]$Level
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    Exit
    
    
    

    It's half way there !
      My Computer


  5. Posts : 3,275
    Win10
       #85

    Paul;

    At line 5 in the script, you are setting the variable Level whilst still in the Command Line context. Somehow, you need to pass that varaible value to the PowerShell starter in line 6.
    eg: this works although it needs a bit of cleaning up.
    Set_Vol_var.BAT
    Code:
    @echo off
    set "Source=%~0"
    set "Function=%Temp%\Speaker_Volume.ps1"
    set Level=0
    set /p "Level=Enter Level: "
    PowerShell "Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File %Function%  %Level% ' -Verb RunAs -Windowstyle Hidden"
    del %Function% 2>nul & MORE /E +10 "%Source%" > %Function%
    
    ::pause
    
    
    ForEach ($arg in $args) {
             $Level =$arg
             }
    Function Set-SpeakerVolume{
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent,
      [Int]$Level
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    Exit
    Last edited by das10; 22 Jul 2022 at 04:54. Reason: added: Verb RunAs
      My Computers


  6. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #86

    Thank you.

    das10 said:
    eg: this works although it needs a bit of cleaning up.
    Actually, it runs OK.

    Obviously with the Pause commented out, you do get a screen flicker, but because my actual code has a Pause [ or similar ] at the end of the Batch Script, the flickering disappears. What 'Cleaning Up' are you referring to please?
      My Computer


  7. Posts : 3,275
    Win10
       #87

    I think this section of the code could be abbreviated or eliminated "if" it were possible to run the function script with the $Level variable which is already set in the PS starter command in the batch section, so it doesn't need any duplication (though I am not sure if it is possible or not).

    Code:
    ForEach ($arg in $args) {
             $Level =$arg
             }
    I think this part of the code is useful when there is a whole series (array) of variables to be processed one by one, and in our script there will only one variable to be processed. Hence the question about a cleanup, were it possible - otherwise it works as is.


    pn: Do you still get a flicker if you use:
    Code:
    PowerShell "Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File %Function%  %Level% ' -Verb RunAs -Windowstyle Hidden" && Exit
    or, do you want to keep the Cmd box open?
    Last edited by das10; 22 Jul 2022 at 06:47.
      My Computers


  8. Posts : 989
    Microsoft Windows 10 Home
       #88

    Paul Black said:
    ...This works in PowerShell . . .

    Code:
    
    Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match "NTFS"} | Sort {$_.Name} | ForEach-Object {echo "$(echo $_.Name) [$(echo $_.Label)]"}
    
    \\?\Volume{30cf9d4a-0000-0000-0000-100000000000}\ [System Reserved]
    C:\ [Win_10]
    D:\ [Data]
    S:\ [System_Image]

    I have tried converting it into a CMD. Here are some of the things that I have tried, and they all produce the correct results except that EACH result is spread over TWO lines, as opposed to ONE line for each as per the PowerShell output . . .

    Code:
    
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object "{echo "$(echo $_.Name) [$(echo $_.Label)]"}""
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object {echo "$(echo $_.Name) [$(echo $_.Label)]"}"
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object "{echo $(echo $_.Name), [$(echo $_.Label)]}"
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object "{echo $(echo $_.Name) [$(echo $_.Label)]}""
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object {echo '$(echo $_.Name) [$(echo $_.Label)]'}"
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object {echo "$^(echo $_.Name^) [$^(echo $_.Label^)]"}"
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object {echo "$(echo $_.Name)" + "[$(echo $_.Label)]"}"
    PowerShell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -Match 'NTFS'} | Sort {$_.Name} | ForEach-Object {echo "$(echo $_.Name)": + "[$(echo $_.Label)]"}"
    
    
    Output over TWO lines from my attempts above . . .

    Code:
    
    \\?\Volume{30cf9d4a-0000-0000-0000-100000000000}\
    [System Reserved]
    C:\
    [Win_10]
    D:\
    [Data]
    S:\
    [System_Image]

    I have often converted code without any problems, but the introduction of echo has got me stumped on this one. The TWO lines for EACH result output needs to be connected together somehow. I am sure that it has something to do with the doubling up of Parenthesis or something similar.

    Thanks.


    The -f Format operator is your friend!

    Code:
    powershell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -eq 'NTFS'} | Sort Name | ForEach-Object {'{0} [{1}]' -f $_.Name , $_.Label }"
    Code:
    C:\>powershell "Get-wmiObject Win32_Volume | Where-Object {$_.FileSystem -eq 'NTFS'} | Sort Name | ForEach-Object {'{0} [{1}]' -f $_.Name , $_.Label }"
    \\?\Volume{1d2da924-dbfa-41bb-a5b4-95dc9aeadf23}\ [Windows RE tools]
    C:\ [Windows]
    D:\ [RECOVERY]
    
    C:\>



    Two separate commands work. The output should be OS Manufacturer:MicrosoftCorporation . . .

    Code:

    PowerShellWrite-Host"' OS Manufacturer : '"
    OS
    Manufacturer:

    C
    :\Windows\system32>PowerShell"Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Manufacturer | Format-Table -HideTableHeaders"


    MicrosoftCorporation
    Code:
    C:\>PowerShell "' OS Manufacturer      : {0}' -f (Get-CimInstance -ClassName Win32_OperatingSystem).Manufacturer
    OS Manufacturer      : Microsoft Corporation
    
    C:\>


    Code:
    PowerShell"(Get-CimInstance -ClassName Win32_OperatingSystem).Name"

    MicrosoftWindows10Pro|C:\Windows|\Device\Harddisk0\Partition2

    ------------------------------------------
    ---You could of course, also usethis---
    ------------------------------------------

    PowerShell"Get-CimInstance -ClassName Win32_OperatingSystem | Select Name"

    MicrosoftWindows10Pro|C:\Windows|\Device\Harddisk0\Partition2


    I would like the output to just be \Device\Harddisk0\Partition2

    Code:
    PowerShell "(Get-CimInstance -ClassName Win32_OperatingSystem).Name.Split('|')[-1]"
    Code:
    C:\>PowerShell "(Get-CimInstance -ClassName Win32_OperatingSystem).Name.Split('|')[-1]"
    \Device\Harddisk0\Partition3
    
    C:\>




    Code:

    PowerShellWrite-Host"'Disk and Partition : Disk ' -NoNewline; Get-Partition | Where-Object {$_.IsBoot -eq 'True'} | Select DiskNumber -ExpandProperty DiskNumber"

    DiskandPartition:Disk0

    Code:

    PowerShellWrite-Host"'Disk and Partition : Partition ' -NoNewline; Get-Partition | Where-Object {$_.IsBoot -eq 'True'} | Select PartitionNumber -ExpandProperty PartitionNumber"

    DiskandPartition:Partition2


    Then I thought that I would try and get them BOTH on the same line, so it looks like this . . .

    DiskandPartition:Disk0Partition2
    Code:
    PowerShell "'Disk and Partition : Disk{0}Partition{1}' -f ( ($p = Get-Partition | ? {$_.IsBoot} ).DiskNumber , $p.PartitionNumber )"
    Code:
    C:\>PowerShell "'Disk and Partition : Disk{0}Partition{1}' -f ( ($p = Get-Partition | ? {$_.IsBoot} ).DiskNumber , $p.PartitionNumber )"
    Disk and Partition : Disk0Partition3
    
    C:\>


    How do I get the following to show . . .

    Code:

    SerialNumber: YL9Q402541
    ReleaseDate:02/04/201919:00:00[TextHere]
    Status: OK


    Code:
    powershell "Get-wmiObject Win32_BIOS | %{([PSCustomObject]@{'Serail Number' = $_.SerialNumber;'Release Date'= '{0}  [ {1} ]' -f $_.ConvertToDateTime($_.ReleaseDate),'Some Text';'Status'=$_.Status}).PSObject.Properties | %{ ' {0,-20}: {1}' -f $_.Name , $_.Value }  }"
    Code:
    C:\>powershell "Get-wmiObject Win32_BIOS | %{([PSCustomObject]@{'Serail Number' = $_.SerialNumber;'Release Date'= '{0}  [ {1} ]' -f $_.ConvertToDateTime($_.ReleaseDate),'Some Text';'Status'=$_.Status}).PSObject.Properties | %{ ' {0,-20}: {1}' -f $_.Name , $_.Value }  }" 
    Serail Number       : 8CG7390CPZ 
    Release Date        : 6/21/2018 07:00:00 PM  [ Some Text ] 
    Status              : OK
    
    C:\>

      My Computer


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #89

    Very strange, there seems to be a couple of posts missing in answer to das10 & KeithM.

    Anyway, I just ran the Script and it now gives the following error, which is strange as it has worked up to now ! . . .

    Code:
    
    Enter Level: 25
    Start-Process : This command cannot be run due to the error: No application is associated with the specified file for
    this operation.
    At line:1 char:1
    + Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
    
    Press any key to continue . . .

    Code:
    
    @echo off
    set "Source=%~0"
    set "Function=%Temp%\Speaker_Volume.ps1"
    set Level=0
    set /p "Level=Enter Level: "
    PowerShell "Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File %Function%  %Level% ' -Windowstyle Hidden"
    del %Function% 2>nul & MORE /E +10 "%Source%" > %Function%
    
    pause
    
    ForEach ($arg in $args) {
             $Level =$arg
             }
    Function Set-SpeakerVolume{
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent,
      [Int]$Level
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    cmd /c del %Function%
    Exit
    
    
    

    This is the file in the &Temp% directory . . .

    Code:
    
    ForEach ($arg in $args) {
             $Level =$arg
             }
    Function Set-SpeakerVolume{
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent,
      [Int]$Level
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    cmd /c del %Function%
    Exit

    My sound is working without any problems !
      My Computer


  10. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #90

    CRACKED IT:

    I approached this from a different angle [ Call ] and got it to work using the following . . .

    Code:
    
    @echo off
    set "Source=%~0"
    set "Function=%Temp%\Speaker_Volume.ps1"
    set Level=0
    set /p "Level=Enter Level: "
    ::   PowerShell "Start-Process PowerShell -ArgumentList '-ExecutionPolicy ByPass -File %Function% %Level%' -Windowstyle Hidden"
    del %Function% 2>nul & MORE /E +11 "%Source%" > %Function%
    Call PowerShell -ExecutionPolicy ByPass -File %Function% %Level%
    
    pause
    
    ForEach ($arg in $args) {
             $Level =$arg
             }
    Function Set-SpeakerVolume{
    Param (
      [Switch]$Min,
      [Switch]$Max,
      [Int]$Percent,
      [Int]$Level
    )
      $wshShell = New-Object -Com WScript.Shell
      if ($Min){
          1..50 | % {$wshShell.SendKeys([Char]174)}
      }
      elseif ($Max){
          1..50 | % {$wshShell.SendKeys([Char]175)}
      }
      elseif($Percent){
          1..50 | % {$wshShell.SendKeys([Char]174)}
          1..($Percent/2) | % {$wshShell.SendKeys([Char]175)}
      }
      else{
          $wshShell.SendKeys([Char]173)
      }
    }
    Set-SpeakerVolume -Percent $Level
    cmd /c del %Function%
    Exit
    
    
    

    Typical, I was mucking around for two hours BEFORE posting and then sorted it out in about 15 minutes.

      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 08:17.
Find Us




Windows 10 Forums