Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

How do I get a Windows batch file to work under Powershell and normal cmd.exe?

I have a batch that performs normal operations. One line of this script runs a Powershell-specific command like this:

powershell -Command "& {Get-FileHash myfile.fwpkg -Algorithm MD5}"

This works when I run the command via the normal Windows Command Prompt cmd.exe.

However, if I run this command under Powershell, this line (ironically) fails:

Get-FileHash : The term 'Get-FileHash' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:4
+ & {Get-FileHash myfile.fwpkg -Algorithm MD5}
+    ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-FileHash:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

How can I get this batch file to work under both Powershell and cmd.exe?

How to call a Perl script with couple of arguments with html tag from within a Python script using Subprocess/shell? HTML tagging not working

Within a python script I have a function which looks as below:

def NotifierFunc(Mail, Ticket_Num, Open_DtTime):
    cmd = "/home/path/MyPerlScript.pl --channel 'MyChannel' --userid 'itsme' --message 'Hello \<at\>"+Mail+"\<at\> : The ticket \<a href='`echo 'http://snview/'"+Ticket_Num+">`echo "+Ticket_Num+"\`\</a\> is not closed for more than 72 hours : Open since "+Open_DtTime+" IST. Please close asap.' "
    print(cmd)
    subprocess.Popen(cmd, shell= True, stout=subprocess.PIPE)

This command does not get executed successfully in the shell primarily because the the message (all that is passed after '--message' is put within single quotes). When I put that under double quotes python script throws syntax error. When I put the whole cmd value in either single quote or doc string quote and message content in double quote that results in syntax error as well. When I run the cmd directly on linux shell with the message content placed within double quotes it works absolutely fine.

Now my question is how can I pass on this whole cmd value to subprocess so that the content of '--message' can be rendered correctly - or how can put the '--message' content within double quotes and make it work. Please note if I pass on the cmd as below - it works fine but it does not have the hyperlink for the ticket number. But I need the hyperlink.

cmd = "/home/path/MyPerlScript.pl --channel 'MyChannel' --userid 'itsme' --message 'Hello \<at\>"+Mail+"\<at\> : The ticket \+Ticket_Num+" is not closed for more than 72 hours : Open since "+Open_DtTime+" IST. Please close asap.' "

I have also tried putting the variables within {} and have tried various quote combinations. Nothing works so far. I am using python 3.10

Error: Resource type '/microsoft.operationalinsights/workspace' is invalid for property 'properties.workspaceId'

I'm trying to create a function app and update the diagnostic settings through PowerShell script to existing Log Analytics Workspace that is residing in different subscription. However throwing error while updating with below error,

ERROR: Resource type '/microsoft.operationalinsights/workspace' is invalid for property'properties.workspaceId'. Expected types are 'microsoft.operationalinsights/workspaces.

It has something to do with the workspace ID as I noticed hardcoding the workspace ID successfully retrieves and update the diagnostic settings.

PowerShell Script:

[CmdLetBinding()]

param(

[string] $SubscriptionId,
[string] $BusinessFunAppRGName,
[string] $BusinessFunAppName,
[string] $laWorkspaceRGName,
[string] $laWorkspaceName,
[string] $lawSubscriptionId

)

$ResourceId = "/subscriptions/$SubscriptionId/resourceGroups/$BusinessFunAppRGName/providers/Microsoft.Web/sites/$BusinessFunAppName"

$diagSettings = Get-AzDiagnosticSetting -ResourceId $ResourceId

if($diagSettings) { Write-Host "Diagnostics Settings Found" } else { Write-Host "Diagnostics Settings Not Found, Applying settings..."

$metric = @()
$log = @()
$metric += New-AzDiagnosticSettingMetricSettingsObject -Enabled $true -Category AllMetrics
$log += New-AzDiagnosticSettingLogSettingsObject -Enabled $true -Category FunctionAppLogs

New-AzDiagnosticSetting -Name "DiagnosticSettings" `
    -ResourceId $ResourceId `
    -Log $log -Metric $metric `
    -WorkspaceId "/subscriptions/$lawSubscriptionId/resourceGroups/$laWorkspaceRGName/providers/microsoft.operationalinsights/workspaces/$laWorkspaceName" `


                  
        
Write-Host "Done"

}

Workflow file sample:

    - name: 'Update diagnostics settings' 
  uses: azure/powershell@v1
  with:
      azPSVersion: "latest"
      inlineScript: |

        arm-templates/business-subscription-afna/update-diagnostics.ps1 `
          -SubscriptionId ${{ github.event.inputs.businessSubscriptionId }} `
          -BusinessFunAppRGName ${env:parameters.resourceGroup.value} `
          -BusinessFunAppName ${env:parameters.funcAppName.value} `
          -laWorkspaceRGName ${env:parameters.mgmtlaWorkspaceRGName.value} `
          -laWorkspaceName ${env:parameters.mgmtlaWorkspaceName.value} `
          -lawSubscriptionId ${env:parameters.mgmtlawSubscriptionId.value} 
      failOnStandardError: $true

unable to capture the output of a remote command in powershell

I am connecting to a windows device from the host using Open-Device in Powershell. i have to run a command in remote PC and capture the output into a variable to process further, unable to do so. tried redirecting using 'Out-String', 'Tee' Operation, Redirect the out put to a file, extract data from STDOUT etc. nothing seems to be working.

Output is printing onto the STDOUT but could not extract

Code i tried.

Open-Device <Device Serial Number>
cmdd dir

GetAccessTokenForRequestAsync fails to return a token for "https://www.googleapis.com/auth/spreadsheets"

Every other scope I try works with out issue. It is possible to interact with the API directly through the Google.Apis.Sheets.v4.SheetsService, but I want to extract the token.

I realize there may be another way to accomplish this, but I am trying to determine why this singular scope is behaving this way.

($SheetsService.HttpClientInitializer.GetAccessTokenForRequestAsync()).Result

returns nothing if the scope specified in Google.Apis.Auth.OAuth2.ServiceCredential that the SheetsService is generated from is https://www.googleapis.com/auth/spreadsheets, but again fails in no other identifiable circumstance.

Why will the second code block in powershell not execute?

The first Script-block is executing without any issues however I am unsure why the second Script-block is not executing.

Import-Module ThreadJob

$streamMonitor = @(
    @{
        directoryPath       = "C:\path\stream1"
        scriptToRestartPath = "C:\path\stream1.bat"
    }
    @{
        directoryPath       = "C:\path\stream2"
        scriptToRestartPath = "C:\path\stream2.bat"
    }

)


$streamMonitor | ForEach-Object {
    Start-ThreadJob -ScriptBlock {
        . C:\Users\luke_linwood_lopez\Desktop\ffmpeg-6.0-essentials_build\bin\streamMonitor.ps1
        streamMonitor @using:_
    } -ArgumentList @($directoryPath, $scriptToRestartPath)
} | Receive-Job -Wait -AutoRemoveJob


#This is the Block that will not execute
Start-ThreadJob -ScriptBlock {
    while ($true) {
        1..3 | ForEach-Object {
            $directory = "C:\nginx\srv\hls\stream$_"
            Get-ChildItem $directory -Recurse -File | Where-Object {($_.Extension -ne ".m3u8") -and $_.CreationTime -lt (Get-Date).AddSeconds(-30)} | Remove-Item -Force
        }
        Write-Host "Completed cleaning directories."
        Start-Sleep -Seconds 30  # Wait for 30 seconds before running the command again
    }
} | Receive-Job -Wait -AutoRemoveJob

Create Grouped Averages from CSV data using Powershell

I have a CSV file similar to this:

Name,Dates,Sortino,RoMDD,PctWins
AAL,2017-06-01 - 2018-06-01,-0.47,-0.26,66.67%
AAL,2017-09-01 - 2018-06-01,0.03,0.02,66.67%
AAL,2017-12-01 - 2018-06-01,-1.05,-0.69,50.00%
AAL,2018-03-01 - 2018-06-01,-2.99,-2.32,33.33%
AAPL,2017-06-01 - 2018-06-01,2.01,1.79,66.67%
AAPL,2017-09-01 - 2018-06-01,1.89,1.68,66.67%
AAPL,2017-12-01 - 2018-06-01,1.66,1.64,66.67%
AAPL,2018-03-01 - 2018-06-01,3.1,3.86,66.67%
ADBE,2017-06-01 - 2018-06-01,3.88,6.09,75.00%
ADBE,2017-09-01 - 2018-06-01,4.16,7.03,77.78%
ADBE,2017-12-01 - 2018-06-01,3.92,8.34,83.33%
ADBE,2018-03-01 - 2018-06-01,4.58,10.29,100.00%

The Dates column is irrelevant and just added for context.

I need to create averages grouped by name, equivalent to an Excel pivot table with Rows: Name and Values: Average of [RoMDD,PctWins,Sortino].

Those results then need to be filtered by Top 20 Sortino, then Top 20 RoMDD, and Top 20 PctWins, all within the previous level (each filter builds on the filtering of the previous level). But that is perhaps a subject for a 2nd post. Otherwise I'll Google (add rank to a list of numbers?)

Here is what I've written so far:

$csv = import-csv -path C:\Temp\Example.CSV | Select-Object -Property Name,Sortino,RoMDD,PctWins | ForEach {$_.PctWins=$_.PctWins.Replace("%","");$_}
$collection = $csv | group-object -property Name
$avg = $collection | Foreach {
    $avg = $_.group|measure -Property Sortino -Average | select -expand average
    add-member -inputobject $_.group[0] -notepropertyname AvgSortino -notepropertyvalue $avg -PassThru
    $avg = $_.group|measure -Property RoMDD -Average | select -expand average
    add-member -inputobject $_.group[0] -notepropertyname AvgRoMDD -notepropertyvalue $avg -PassThru
    $avg = $_.group|measure -Property PctWins -Average | select -expand average
    add-member -inputobject $_.group[0] -notepropertyname AvgPctWins -notepropertyvalue $avg -PassThru
} | Select Name,AvgSortino,AvgRoMDD,AvgPctWins

This is close, but adds each average to a new object, instead of additional columns in the one object.

Would creating a new PSCustomObject be a better approach here? Or using Datatable and Linq? I don't know Linq but I know SQL quite well.

The final output will be written back to a new CSV file including new (Avg) column headers.

Add and Replace Jobs in Crontab [duplicate]

I have a list of cron jobs that already exists, below are some of them

*/30 * * * * /path/to/script.sh
0 12 * * * /path/to/script_2.sh
* * * * /path/to/script_3.sh

Scenario:

  1. script_2.sh is not found on some of the servers.
  2. Need to add >/dev/null 2>&1 to all of this script

How can I do in Linux script to search for script_2.sh if that is already present in the crontab? If it is present in the crontab, >/dev/null 2>&1 should be appended. If it doesn't exist, it should be added to the jobs.

Capturing first line from shell command output and continue processing rest

I'm trying to capture and "store away" (preferably in a variable) the first line of a command output and forward the remainder of the output back to stdin of the next command in a pipe. As an FYI, the purpose is to capture and store the cursor reference of a redis-cli SCAN invocation, while processing the data returned.

This is what I came up with:

command | { read first_line ; cat ; } | process_rest
echo $first_line

This seems to work as intended in zsh, but not in Bourne Shell ("/bin/sh") or bash. It appears that piping to the command grouping causes a subshell in sh/bash, "swallowing" the first_line variable.

What is the best way to achieve what I'm trying to do? Will I need to "pipe" the first line through a file or fifo instead of trying to set a variable? I also tried using a while read line ; do construct to filter out the first line and continue processing the rest, but it appears it has the same effect (any variable changes within the do/done block are not reflected in the outer scope).

How to send data to local clipboard from a remote SSH session

Most Unix-like systems have a command that will let you pipe/redirect output to the local clipboard/pasteboard, and retrieve from same. On OS X, these commands are

pbcopy, pbpaste 

Is there a way to replicate this functionality while SSHed into another server? That is,

  1. I'm using Computer A.
  2. I open a terminal window
  3. I SSH to Computer B
  4. I run a command on Computer B
  5. The output of Computer B is redirected or automatically copied to Computer A's clipboard.

And yes, I know I could just (shudder) use my mouse to select the text from the command, but I've gotten so used to the workflow of pipping output directly to the clipboard that I want the same for my remote sessions.

Code is useful, but general approaches are appreciated as well.

Zlib compression in Powershell

I need to compress a string to Zlib in PowerShell.

Is there a way we can compress using the ZLibStream class in PowerShell?

https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zlibstream?view=net-8.0

I was able to compress using the class DeflateStream and GZipStream, but the external system which uses the data was not able to decode it.

This is the powershell script I tried with

$stringToCompress  = 'string to compress'

$memStream = New-Object System.IO.MemoryStream
$deflateStream = New-Object System.IO.Compression.GZipStream($memStream, [System.IO.Compression.CompressionMode]::Compress)
$streamWriter = New-Object System.IO.StreamWriter($deflateStream)
$streamWriter.Write($stringToCompress)
$streamWriter.Close();
$encodedCompressedString = [System.Convert]::ToBase64String($memStream.ToArray())
Write-Output $encodedCompressedString 

In C#, DirectoryEntry is returning empty AuditRules collection, even though audit rules do exist

I am attempting to create a C# console application to manage audit permissions on a domain (not file system). My problem is that I cannot read the existing audit rules in C#, I only get an empty collection, even though I am certain there are audit rules in the domain.

I am needing to manage over 100+ independent domains, so I want to come up with a programmatic solution to help manage them.

I can do this without issue in PowerShell using the following command.

$acl = Get-Acl -Path "DC=mydomain,DC=local" -Audit
$acl.Audit

This returns a collection of 5 audit rules. However, when trying to perform the same operation in C#, the audit rules are not returned.

string path = "LDAP://mydomain.local/DC=mydomain,DC=local";
DirectoryEntry domain = new DirectoryEntry(path);
               
AuthorizationRuleCollection authorizationRuleList1 = domain.ObjectSecurity.GetAuditRules(true, true, typeof(NTAccount));
AuthorizationRuleCollection authorizationRuleList2 = domain.ObjectSecurity.GetAuditRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));

Console.WriteLine("Authorization Rules 1: " + authorizationRuleList1.Count);
Console.WriteLine("Authorization Rules 2: " + authorizationRuleList2.Count);

Running the above shows aa count of 0 audit rules. Oddly, the access rules are returned without issue.

I have tried

  1. Explicitly applying a password for the DirectoryEntry initialization
  2. Running "run as administrator"
  3. Verifying the audit permissions exist by using LDAP explorer

Any pointers would be much appreciated.

Updated code using ActiveDs to get security descriptor.

string path = "LDAP://demo.local/DC=demo,DC=local";
DirectoryEntry domain = new DirectoryEntry(path);
var ntSecurityDescriptor = domain.Properties["ntSecurityDescriptor"];

ActiveDs.ADsSecurityUtility secUtility = new ActiveDs.ADsSecurityUtility();
ActiveDs.IADsSecurityDescriptor sd = (IADsSecurityDescriptor)ntSecurityDescriptor[0];
ActiveDs.IADsAccessControlList aclList =  ActiveDs.IADsAccessControlList)sd.DiscretionaryAcl;

why if in C# i invoke a script with powershell an i capture the buffer is completely different running from windows terminal PS

why if in C# i invoke a script with powershell an i capture the buffer is completely different running from windows terminal PS

i run this on C# application open with elevated privileges:


string command = "docker-compose -p wsdd-projects -f C:\Docker-Structure\init.yml up -d --build"

string policy = "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force";

string arguments = $"-NoProfile -Command \"{policy} ; '{command}'\"";

ProcessStartInfo processBuilder = new("powershell")
{
    Arguments = arguments,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    Verb = "runas",
    CreateNoWindow = true,
    StandardOutputEncoding = Encoding.UTF8,
    StandardErrorEncoding = Encoding.UTF8
};

capture output with:

Process process = new() { StartInfo = processBuilder };

process.OutputDataReceived += (sender, e) =>
{
    if (!string.IsNullOrEmpty(e.Data))
    {
        if (showOutput) { terminal.AddToTerminal(e.Data); }
        output.AppendLine(e.Data);
    }
};

process.ErrorDataReceived += (sender, e) =>
{
    if (!string.IsNullOrEmpty(e.Data))
    {
        if (showOutput) { terminal.AddToTerminal(e.Data); }
        output.AppendLine(e.Data);
    }
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

await process.WaitForExitAsync();

i get this output from PS invoke from C#:

 reverse-proxy Pulled
 phpmyadmin Pulled
 faef57eae888 Pull complete
 989a1d6c052e Pull complete
 0705c9c2f22d Pull complete
 621478e043ce Pull complete
 98246dcca987 Pull complete
 bfed8c155cb6 Pull complete
 7a7c2e908867 Pull complete
 d176994b625c Pull complete
 2d8ace6a2716 Pull complete
 c70df516383c Pull complete
 15e1b44fe4c7 Pull complete
 65e50d44e95a Pull complete
 77f68910bc0a Pull complete
 605dd3a6e332 Pull complete
 99ce27188f07 Pull complete
 74d64e32c5d5 Pull complete
 ef5fc9928b9f Pull complete
 163f3256e112 Pull complete
 8a1e25ce7c4f Pull complete
 e78b137be355 Pull complete
 39fc875bd2b2 Pull complete
 035788421403 Pull complete
 87c3fb37cbf2 Pull complete
 c5cdd1ce752d Pull complete
 33952c599532 Pull complete
 80bad5b02d6c Pull complete
 b86cede81497 Pull complete
 876ba35ce95b Pull complete
 ac8fec215cc8 Pull complete
 7795496aa545 Pull complete
 4f4fb700ef54 Pull complete

Instead of this output in Terminal PS Windows:

[+] Running 33/33
 ✔ reverse-proxy 13 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                   14.3s
   ✔ 8a1e25ce7c4f Pull complete                                                      8.1s
   ✔ e78b137be355 Pull complete                                                      8.5s
   ✔ 39fc875bd2b2 Pull complete                                                      6.9s
   ✔ 035788421403 Pull complete                                                      7.6s
   ✔ 87c3fb37cbf2 Pull complete                                                      8.1s
   ✔ c5cdd1ce752d Pull complete                                                      8.6s
   ✔ 33952c599532 Pull complete                                                      8.7s
   ✔ 80bad5b02d6c Pull complete                                                      9.0s
   ✔ b86cede81497 Pull complete                                                      9.4s
   ✔ 876ba35ce95b Pull complete                                                      9.8s
   ✔ ac8fec215cc8 Pull complete                                                      9.6s
   ✔ 7795496aa545 Pull complete                                                      10.1s
   ✔ 4f4fb700ef54 Pull complete                                                      10.2s
 ✔ phpmyadmin 18 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                  18.4s
   ✔ faef57eae888 Pull complete                                                      2.3s
   ✔ 989a1d6c052e Pull complete                                                      0.5s
   ✔ 0705c9c2f22d Pull complete                                                      4.7s
   ✔ 621478e043ce Pull complete                                                      1.0s
   ✔ 98246dcca987 Pull complete                                                      3.0s
   ✔ bfed8c155cb6 Pull complete                                                      2.8s
   ✔ 7a7c2e908867 Pull complete                                                      3.5s
   ✔ d176994b625c Pull complete                                                      4.3s
   ✔ 2d8ace6a2716 Pull complete                                                      4.1s
   ✔ c70df516383c Pull complete                                                      5.1s
   ✔ 15e1b44fe4c7 Pull complete                                                      4.8s
   ✔ 65e50d44e95a Pull complete                                                      5.3s
   ✔ 77f68910bc0a Pull complete                                                      5.4s
   ✔ 605dd3a6e332 Pull complete                                                      5.9s
   ✔ 99ce27188f07 Pull complete                                                      6.1s
   ✔ 74d64e32c5d5 Pull complete                                                      6.5s
   ✔ ef5fc9928b9f Pull complete                                                      6.4s

where is the line this: [+] Running

and the characters: and [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]

❌
❌