Convert PS to CMD

Page 24 of 24 FirstFirst ... 14222324

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

    OK, I am having trouble with this.

    These ALL work in Batch:

    Code:
    
    set "MAX_Events=2,000"
    
    echo    - blah EXCEEDS [%MAX_Events%]...
    PowerShell Write-Host """"   "- blah EXCEEDS [%MAX_Events%]..."""
    PowerShell -ExecutionPolicy Bypass -Command """"   "- blah EXCEEDS [%MAX_Events%] ..."""
    
    RESULT for ALL THREE:
    
    - blah EXCEEDS [2,000] ...

    BUT, how do I get the %MAX_Events% to work in this please ?:

    Code:
    
         $Security_Log = (Get-WinEvent -ListLog 'Security').RecordCount; ^
         if ($Security_Log -gt %MAX_Events%) {Write-Host """`n blah EXCEEDS [%MAX_Events%] ..."""; exit 1} else { ^

    I do NOT want the Script re-written, I just need to know how to get the %MAX_Events% as a CMD.

    I have tried things like cmd.exe /c etc, but to NO avail.

    Thanks.



    EDIT:

    This works except the format is lost:

    Code:
    
    set "MAX_Events=2,000"
    
        $Security_Log = (Get-WinEvent -ListLog 'Security').RecordCount; ^
         if ($Security_Log -gt """%MAX_Events%""") {Write-Host """`n blah EXCEEDS ["""%MAX_Events%"""] ..."""; exit 1} else { ^
    
    RESULT:
    
    blah EXCEEDS [ 2 000]
    

    I know I can use:

    Code:
    
         $MAX_Events=(2000).ToString('#,##0'); ^
         $Security_Log = (Get-WinEvent -ListLog 'Security').RecordCount; ^
         if ($Security_Log -gt $MAX_Events) {Write-Host """`n blah EXCEEDS [$MAX_Events] ..."""; exit 1} else { ^
    

    BUT, I want to use the Batch variable %MAX_Events% so I do NOT need to hard code the 2,000 twice for example.

    OR, thinking about it, if it is easier, perhaps use a PS variable and then use it in the Batch !!!
    Last edited by Paul Black; 25 Aug 2023 at 15:44.
      My Computer


  2. Posts : 782
    Windows 7
       #232

    There's two separate problems in this script.

    1. PS doesn't recognize 2,000 as a string, unless it's quoted. You're actually passing an array of values 2 & 0(00)
    Code:
    $Max_Events = 2,000
    $Max_Events
    
    $Max_Events = '2,000'
    $Max_Events
    Code:
    2
    0
    2,000

    2. Be careful with CMD escaping of special characters.
    Code:
    set "MAX_Events=2,000"
    
    powershell "'Pass 1'; if ('%Max_Events%' -gt 1000) { write-host """"%Max_Events% is greater than 1000"""" }"
    powershell "$MAX_Events=%MAX_Events%;   'Pass 2'; if ($Max_Events -gt 1000) { write-host """"$Max_Events is greater than 1000"""" }"
    powershell "$MAX_Events='%MAX_Events%'; 'Pass 3'; if ($Max_Events -gt 1000) { write-host """"$Max_Events is greater than 1000"""" }"
    Code:
    set "MAX_Events=2,000"
    
    powershell "'Pass 1'; if ('2,000' -gt 1000) { write-host """"2,000 is greater than 1000"""" }"
    Pass 1
    2,000 is greater than 1000
    
    powershell "$MAX_Events=2,000;   'Pass 2'; if ($Max_Events -gt 1000) { write-host """"$Max_Events is greater than 1000"""" }"
    Pass 2
    
    powershell "$MAX_Events='2,000'; 'Pass 3'; if ($Max_Events -gt 1000) { write-host """"$Max_Events is greater than 1000"""" }"
    Pass 3
    2,000 is greater than 1000
      My Computer


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

    WOW, thanks @garlin,

    I have it working now:

    Code:
    
    set "MAX_Events=2,000"
    
         $MAX_Events='%MAX_Events%'; ^
         $Security_Log = (Get-WinEvent -ListLog 'Security').RecordCount; ^
         if ($Security_Log -gt $Max_Events) {Write-Host """`n blah EXCEEDS [$Max_Events] ..."""; exit 1} else { ^

    Thanks again.
      My Computer


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

    I have a Script that runs an .exe file and outputs the data to a %Temp%\A.txt file. As an example of the %Temp%\A.txt file:

    Code:
    
       blah
    
         blah: blah
            blah: blah
        blah:     blah   (blah)
    
       blah:   blah (blah: blah)                               
    
         blah:    blah blah (blah: blah)                               
    blah:     blah
     Result URL: https://www.tenforums.com/

    How can I extract JUST the URL to another %Temp%\B.txt file and then get it to open in my Browser [ Chrome ] please ?

    I have tried Get-Content, regex, and Invoke-WebRequest but I can't seem to get this right.

    I can get it to partly extract using this:

    Code:
    
    PowerShell Get-Content %Temp%\A.txt ^| Where { $_.Contains('https') } ^| Set-Content %Temp%\B.txt
    
    
    
    OUTPUT to %Temp%\B.txt:
    
     Result URL: https://www.tenforums.com/

    Thanks in advance.
    Last edited by Paul Black; 15 Sep 2023 at 19:16.
      My Computer


  5. Posts : 782
    Windows 7
       #235

    Code:
    Get-Content '.\blah.txt' |% {
        if ($_ -match '(http(|s):(\S+))(\s?)') {
            start $Matches[1]
        }
    }

    Let's break this regex down:
    Code:
     
    ( http(|s): (\S+) )  (\s?)
    group 1 [ http: (optional s): (one or more non-whitespace) ]
    group 2 [ zero or more trailing whitespace ] to delimit end of URL

    1. Find matches against group 1, which is a complete URL.
    2. Pass to Explorer (via CMD) as "start url", which opens URL in your default browser.
      My Computer


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

    Thanks as always @garlin,

    This means I do NOT need a secondary file which is great so I can just use %Temp%\A.txt.

    I am however having trouble escaping in order for it to work:

    Code:
    
    PowerShell "Get-Content $Env:TEMP\A.txt | %{if ($_ -match '(http(|s):(\S+))(\s?)') {start $Matches[1]}}"

    Although this works in PS ISE:

    Code:
    
    Get-Content "$Env:TEMP\A.txt" | %{
        if ($_ -match '(http(|s):(\S+))(\s?)') {
            start $Matches[1]
        }
    }

    It is nearly 02:30 so I need to go to bed.
      My Computer


  7. Posts : 782
    Windows 7
       #237

    Code:
    powershell "Get-Content blah.txt |%% {if ($_ -match '(http(|s):(\S+))(\s?)') {start $Matches[1]}}"
      My Computer


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

    garlin said:
    Code:
    powershell "Get-Content blah.txt |%% {if ($_ -match '(http(|s):(\S+))(\s?)') {start $Matches[1]}}"
    Thanks. Of course it is %%.
      My Computer


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

    OK, I am struggling with PS on this one.

    I want to list all the Registry Keys and Sub-Keys Names ONLY in a list like in the below:

    Convert PS to CMD-image.png
    Convert PS to CMD-image.png

    When I use this I can only get the top folders:

    Code:
    
    $NET_Key="HKLM:\SOFTWARE\Microsoft\Net Framework Setup\NDP\*"
    Get-ChildItem $NET_Key -Name
    
    
    
    OUTPUT:
    
    CDF
    v2.0.50727
    v3.0
    v3.5
    v4
    v4.0

    What do I need to do to adapt it to output the correct result please ?

    UPDATE 1:

    Almost got there:

    Code:
    
    $NET_Key="HKLM:\SOFTWARE\Microsoft\Net Framework Setup\NDP\*"
    Get-ChildItem -Recurse $NET_Key -Name
    
    
    
    OUTPUT:
    
    v4.0
    1028
    1029
    1030
    1031
    1032
    1033
    1035
    1036
    1038
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1049
    1053
    1055
    2052
    2070
    3076
    3082
    Servicing
    Setup
    Servicing\Windows Workflow Foundation
    Setup\1033
    Setup\Windows Communication Foundation
    Setup\Windows Presentation Foundation
    Setup\Windows Workflow Foundation
    1033
    Client
    Full
    Client\1033
    Full\1033
    Client
    
    
    

    BUT, it does NOT show the actual folder name [ Parent ] above the respective output !

    UPDATE 2:

    BINGO !

    Code:
    
    $NET_Key="HKLM:\SOFTWARE\Microsoft\Net Framework Setup\NDP"
    Get-ChildItem -Recurse $NET_Key -Name
    
    
    
    OUTPUT:
    
    CDF
    v2.0.50727
    v3.0
    v3.5
    v4
    v4.0
    CDF\v4.0
    v2.0.50727\1028
    v2.0.50727\1029
    v2.0.50727\1030
    v2.0.50727\1031
    v2.0.50727\1032
    v2.0.50727\1033
    v2.0.50727\1035
    v2.0.50727\1036
    v2.0.50727\1038
    v2.0.50727\1040
    v2.0.50727\1041
    v2.0.50727\1042
    v2.0.50727\1043
    v2.0.50727\1044
    v2.0.50727\1045
    v2.0.50727\1046
    v2.0.50727\1049
    v2.0.50727\1053
    v2.0.50727\1055
    v2.0.50727\2052
    v2.0.50727\2070
    v2.0.50727\3076
    v2.0.50727\3082
    v3.0\Servicing
    v3.0\Setup
    v3.0\Servicing\Windows Workflow Foundation
    v3.0\Setup\1033
    v3.0\Setup\Windows Communication Foundation
    v3.0\Setup\Windows Presentation Foundation
    v3.0\Setup\Windows Workflow Foundation
    v3.5\1033
    v4\Client
    v4\Full
    v4\Client\1033
    v4\Full\1033
    v4.0\Client
    
    
    

    I will leave this post here as somebody might find it useful.

    Last edited by Paul Black; 20 Oct 2023 at 07:26.
      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 02:04.
Find Us




Windows 10 Forums