Loading...

Follow PowerShell.org - Forums on Feedspot

Continue with Google
Continue with Facebook
or

Valid

$file= Import-Csv D:\files\users.csv
ForEach ( $user in $file)
{
$i= $user.User + '@' + $user.Domain
$i | Export-Csv D:\files\users1.csv -NoTypeInformation -Append
}

I am trying to combine username with domain name from a csv file. However when I just output the variable it displays the information fine. However when I export to csv it displays the length in the csv file. Any suggestions on what is wrong here?

 

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
PowerShell.org - Forums by Joel /u/ta11ow @vexx32 - 21m ago

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.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Yes there is.  There's a cmdlet called export-csv.  Look for some examples online, and then give it a shot.  If you're still having issues post your script and the error(s).

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I think I see what you're saying. I wouldn't have $_ variable until the whole expression inside -arguments was executed

Yep here,

but its not about the Invoke-Expression, but the double quotes. Just make it single quotes for -argumentlist. Variables wont get invoked in single quotes and here you wont have need escape double quotes for notepad.

–arguments 'Get-Process | Where-Object {$_.ProcessName -like "*notepad*" }'
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I see now. If you use quotes, your local computer will evaluate the $_ and it will throw an error because it has no member ProcessName. But if you use single quotes, it will pass the variable name plain text to the remote computer where it will see that it's a variable.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

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

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

You need to use Select-Object -ExpandProperty SamAccountName; select samaccountname (or more properly Select-Object -Property SamAccountName if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property — which is what Set-ADUser will need.

Hi Joel, i think i do understand what you tell mee but i don't know exaiclly what you mean. I replaced the code with

1
Select-Object –Property SamAccountName
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

But i still receive the same error message:

Cannot bind parameter Identity

Any help for me?

Thanks Dan! This is the way go, script working.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I think I see what you're saying. I wouldn't have $_ variable until the whole expression inside -arguments was executed

Yep here,

but its not about the Invoke-Expression, but the double quotes. Just make it single quotes for -argumentlist. Variables wont get invoked in single quotes and here you wont have need escape double quotes for notepad.

-arguments 'Get-Process | Where-Object {$_.ProcessName -like "*notepad*" }'
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

another thing is that the output is multiplied at least 5 times at this point where for me just 1 time is sufficient.

You have 6 drives listed in your xml file. You are getting 5 error outputs because 5 are failing and 1 is successful.

On the plus side, this means that your foreach loop is executing the expected number of times.

You should apply

Set-PSDebug -Trace 2

and look at the output of each loop to see where it goes wrong.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Nice and thank you but...

Is there a way to get it collected for foreach so that I can export to a csv?

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

you're close...

foreach ($p in $ip1){
$pc=([System.Net.Dns]::GetHostEntry($p).HostName)
write-host "IP = $P, DNS =$PC"}

Read for later

Articles marked as Favorite are saved for later viewing.
close
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview