0

I have a Powershell script that looks for files created in the past 24 hours in a certain folder and then looks for certain filenames which it then outputs to a folder. I would like to output to a hipchat room instead but I'm having trouble passing the variable. My code is below:

$var1 = Get-ChildItem -Path "W:\Blabla" -Recurse | Where-Object { $_.CreationTime -gt (Get-Date).AddHours(-24) } | Select-Object Fullname,CreationTime
Out-File test-24.txt -InputObject $var1

$var2 = Select-String -Path test-24.txt -Pattern "pass|Angie" | select line
Out-File test-out.txt -InputObject $var2



$params = @{
    Uri = "https://hipchat.blabla.com/v2/room/456/notification?auth_token=????"
    Method = "POST"
    Body = @{
        color = 'red'
        message = "$var2"
        notify = $false
        message_format = "text"
    } | ConvertTo-Json
    ContentType = 'application/json'
}

Invoke-RestMethod @params

I'm trying to pass $var2 into the second array but my messages turn out blank. Thank you.

snowman
  • 15
  • 6
  • Can you confirm test-out.txt has a value inside it ? – Sage Pourpre Oct 03 '17 at 18:44
  • Also, I believe your Body value should be between parameters for the parameter to be set properly to the result of that operations. Body = (@{ color = 'red' message = "$var2" notify = $false message_format = "text" } | ConvertTo-Json) – Sage Pourpre Oct 03 '17 at 18:48
  • Yes, test-out.txt has the following: W:\blabla\pass.txt 10/3/2017 9:41:10 AM I tried it your way and I get lots of errors like "Unexpected token 'message' in expression or statement." and "The hash literal was incomplete." – snowman Oct 03 '17 at 19:06
  • It must be something with how I put the data into $var2. If I just define $var2 as "It's working" it works perfectly :( – snowman Oct 03 '17 at 19:31
  • Very Possible... Take a look at: https://stackoverflow.com/questions/29306439/powershell-convertto-json-problen-containing-special-characters . I remember having to use the unescape at some point because of the automatic conversion of some characters. It might get you on the path you seek. – Sage Pourpre Oct 03 '17 at 19:39
  • ``$var2 = $(Get-Content -Path test-24.txt | Select-String -Pattern "pass|Angie") -join "`r`n"`` – JosefZ Oct 03 '17 at 20:03
  • Thanks guys! JosefZ, your solution worked after I modified it. I piped it into Out-String and got it to run. Thanks! – snowman Oct 04 '17 at 18:13

0 Answers0