bash to powershell

Welcome Forums General PowerShell Q&A bash to powershell

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #167335

    Participant
    Topics: 6
    Replies: 2
    Points: 44
    Rank: Member

    Hi all,

    I have bash script and i want to convert into powershell or wirte in powershell can you help me out this below script,

    #!/bin/bash
    
    DESC=$1
    HOSTNAME=$2
    DATE=$3
    REPLACE="-"
    DATE=${DATE//"."/$REPLACE}
    TIME=$4
    SEV=$5
    SEV=${SEV^^}
    IP=$6
    TIME=`date -d "$DATE $TIME" +%s`
    
    NOTES="SEV : $SEV\tDESC: $DESC\tHOSTNAME: $HOSTNAME\tDATE: $DATE $TIME"
    COMP=""
    
    echo "Started at $TIME with $NOTES" >> /tmp/push.log
    if [[ $DESC =~ .*load.* || $DESC =~ .*CPU.* ]]
    then
    COMP="Load"
    fi
    
    if [[ $DESC =~ .*Service.* || $DESC =~ .*running.* ]]
    then
    COMP="Service"
    fi
    
    if [[ $DESC =~ .*PROCS.* || $DESC =~ .*processes.* ]]
    then
    COMP="Process"
    fi
    
    if [[ $DESC =~ .*SWAP.* || $DESC =~ .*Swap.* ]]
    then
    COMP="Swap"
    fi
    
    if [[ $DESC =~ .*DISK.* || $DESC =~ .*inode.* || $DESC =~ .*folder.* ]]
    then
    COMP="Disk"
    fi
    
    if [[ $DESC =~ .*Memory.* || $DESC =~ .*MEMORY.* ]]
    then
    COMP="Memory"
    fi
    
    if [[ $DESC =~ .*Logins.* || $DESC =~ .*Login.* ]]
    then
    COMP="Security"
    fi
    
    if [[ $DESC =~ .*docker.* || $DESC =~ .*Container.* ]]
    then
    COMP="Docker"
    fi
    
    if [[ $DESC =~ .*bandwidth.* || $DESC =~ .*RX.* ]]
    then
    COMP="Interface"
    fi
    
    curl -i -X POST 'http://x.x.x.x:8000/evm/api1.0/endpoints/eventreceiver' -d "{\"ci_name\": \"$HOSTNAME\", \"component\": \"$COMP\", \"description\": \"$DESC\", \"notes\": \"$NOTES\", \"severity\": \"$SEV\", \"event_created_time\": \"$TIME\", \"source\": \"ZABBIX\"}" -H "Content-Type: Application/json" >> /tmp/curl.log 2>> /tmp/curl.log
    
    echo "Finished with $?" >> /tmp/push.log
    
  • #167347

    Participant
    Topics: 2
    Replies: 483
    Points: 1,152
    Helping Hand
    Rank: Community Hero

    Hmm, fairly straightforward, I suppose. Somewhat, at least.

    [CmdletBinding()]
    param(
        [Parameter(Position = 0)]
        $Description,
    
        [Parameter(Position = 1)]
        $Hostname,
    
        [Parameter(Position = 2)]
        $Date,
    
        [Parameter(Position = 3)]
        $Time,
    
        [Parameter(Position = 4)]
        $Severity,
    
        [Parameter(Position = 5)]
        $IpAddress,
    )
    
    if (-not $PSBoundParameters.ContainsKey('Date')) {
        $Date = $Date -replace '.', '-'
    }
    
    $DateTime = "$Date $Time" | Get-Date
    $Severity = $Severity.ToUpper()
    
    $NOTES = "Severity : ${Severity}`tDescription: ${Description}`tHostname: ${Hostname}`tDate: $DateTime"
    
    "Started at $DateTime with $NOTES" | Add-Content -Path /tmp/push.log
    
    $Comp = switch -regex ($Description) {
        '.*(load|CPU).*' { "Load" }
        '.*(Service|running).*' { "Service" }
        '.*(PROCS|processes).*' { "Process" }
        '.*Swap.*' { "Swap" }
        '.*(DISK|inode|folder).*' { "Disk" }
        '.*Memory.*' { "Memory" }
        '.*Logins?.*' { "Security" }
        '.*(docker|Container).*' { "Docker" }
        '.*(bandwidth|RX).*' { "Interface" }
    }
    
    $RestParams = @{
        Method      = 'POST'
        ContentType = 'application/json' 
        Uri         = 'http://x.x.x.x:8000/evm/api1.0/endpoints/eventreceiver'
        Body        = @'
            {
                "ci_name": "$Hostname",
                "component": "$COMP",
                "description": "$Description",
                "notes": "$NOTES", 
                "severity": "$Severity", 
                "event_created_time": "$DateTime", 
                "source": "ZABBIX"
            }
    '@
    }
    Invoke-RestMethod @RestParams 2>&1 | Add-Content -path /tmp/curl.log
    
    "Finished with $?" | Add-Content -Path /tmp/push.log

    More or less, anyway. ^^

    That's the gist of it. Translating `curl` to a proper `Invoke-WebRequest` can sometimes be a bit dicey as they don't have the same design philosophy quite... but worst comes to worst you can just leave the `curl` command as-is and ignore the Restmethod stuff; PS is perfectly capable of invoking `curl` for you should the need arise.

You must be logged in to reply to this topic.