I can't get my head around the piping thing. I've read through the Get-Help and in Jon Dones his examples but I can't understand it properly. So I started playing with a function I found online to execute a script block in a specific culture (regional settings).
I managed to have it process these arguments correctly:
Use-Culture de-DE {Get-Date}, {Get-TimeStamp}
Use-Culture nl-BE {Get-Date}, {Get-TimeStamp}
But when I try to pipe to Use-Culture, it's just not working at all:
de-DE {Get-Date}, {Get-TimeStamp} | Use-Culture
nl-BE {Get-Date} | Use-Culture
I feel like I'm missing the basic concept here. There was another question on StackOverlfow talking about this topic. One guy said to use Foreachand someone else said to work with -Inputobject. Then again, Don Jones used in his Advanced Function no such thing and stated that the Process block gets iterated for each object.
What is best practice to pipe multiple arguments to a function? In this case, multiple Script blocks to execute against the same Culture.
Thank you for your help as always.
Function Use-Culture {
param(
[Parameter(
Mandatory=$true,Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[System.Globalization.CultureInfo]$culture,
[Parameter(
Mandatory=$true,Position=1,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ScriptBlock[]]$code
)
process {
ForEach ($_ in $code) {
trap {
[System.Threading.Thread]::CurrentThread.CurrentCulture = $currentCulture
}
$currentCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
Invoke-Command $_
[System.Threading.Thread]::CurrentThread.CurrentCulture = $currentCulture
}
}
}
Related
Hello I am trying to add a custom dimension or something similar called properties. Below I added a printscreen so something similar found online
Here is the code I used to try and create this
$JSON = #{
Type = 'SQL'
Subscriptionname = "123"
property = #{
SQLServerName = "myServer";
DatabaseName = "myDatabase";
}
}
$json2 = $JSON | ConvertTo-Json
# $json2
# Submit the data to the API endpoint
Post-LogAnalyticsData -customerId $customerId -sharedKey $sharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($json2)) -logType "MyRecordTypetoo"
But the results came out like below
Anyone have any ideas to get this working?
Please the follow the below ways to fix the issue:
Way 1
using keithbabinec/AzurePowerShellUtilityFunctions Git Hub repo
#Import the AzurePowerShell Utility Function file from above github repo
Import-Module AzurePowerShellUtilityFunctions.psd1
# Adding custom properties using Event Telemetry
Send-AppInsightsEventTelemetry -InstrumentationKey '<Instrumentation Key of AI>' -EventName <Event Name> -CustomProperties #{ '<Custom Property>' = '<Property Value>'}
Way 2
Using PSModule
For adding custom dimension in application insights, we have PSModule to add the properties in application Insights. Refer here for the detailed steps.
Way 3
Using RESTAPI
I Hope you already tried with the same. By calling the RestAPI as same mentioned in the above PowerShell module.
Result
I'm pretty stuck and can't find anything about it on the internet. I'm also not sure how to describe the thing i'm looking for, so maybe someone can help me.
I've got some code to create a ticket in TopDesk through API using invoke-restmethod in PS.
For the request field in TopDesk, I need some output stored in a variable, but if I want to use a variable in the PS command, I need to define the JSON body with the use of #{} | covertTo-JSON (found that somewhere on the internet).
Now this parameter I need to put through, has to have a definition. I need to give in in the value is a email or a name.
$json = #{
"callerLookup" = "{ email : email#domain.com }"
} | Convertto-JSON
Now the thing is, TopDesk doesn't see the "{ email : email#domain.com }" as a correct value.
Before, I just the following (which will work, but can't use variables):
$body = '{"email": "automation#rid-utrecht.nl"}'
I hope I described my problem cleary enough and hope that someone can help me.
Thanks in advance.
Kind regards,
Damian
For ConvertTo-Json to produce the serialized { "property" : "value" } syntax, you must pass it an object that has a property called property and an associated value equal to value. You can easily create this scenario with the [pscustomobject] accelerator.
$json = #{
callerLookup = [pscustomobject]#{email = 'email#domain.com'}
} | ConvertTo-Json
I am hitting a Rest API and collected a gnarly block of Json. I'm running convertfrom-json on that to get a powershell object which I would like to manipulate. Essentially I need to prune a number of field/values.
Its no issue to 'get' the fields I want to remove from the object as I can just drill down to the field and collect the value thats easy, where I am stuck is how to trim off that field from the posh object. Would appreciate any assistance. Thanks.
Example:
$sample_json = #"
{
"fields": {
"field_one": 1,
"field_two": 2,
"field_three": "three",
"field_four": "remove_me",
"field_five": 5
}
}
"#
Clear-Host
$json_object = ConvertFrom-Json -InputObject $sample_json
$json_object
Gives:
fields
------
#{field_one=1; field_two=2; field_three=three; field_four=remove_me; field_five=5}
So the question is how can I remove "field_four" key, and it's value, from $json_object ? Apologies if this is crazy simple; I'm a bit out of touch with Powershell these last few years.
You can remove "field_four" with the Remove method from PSObject.Properties:
$json_object.fields.PSObject.Properties.Remove("field_four")
Use the following statement
$json_object.fields.PSObject.Properties.Remove("field_four")
EDIT:
My below question still stands but I appreciate that it's hard to answer without sifting through a pile of code. Therefore, to ask a somewhat similar question, does anyone have any examples of Menhir being used to implement an AST? Preferably not "toy" projects like a calculator but I would appreciate any help I could get.
Original Question:
I'm trying to implement an abstract syntax tree using Menhir and there's an issue I can't seem to solve. My set up is as follows:
The AST's specification is generated using atdgen. This is basically a file with all of my grammar rules translated to to the ATD format. This allows me to serialize some JSON which is what I use to print out the AST.
In my parser.mly file I have a long list of production. As I'm using Menhir I can link these productions up to AST node creation, i.e. each production from the parser corresponds with an instruction to record a value in the AST.
The second point is where I'm really struggling to make progress. I have a huge grammar (the ast.atd file is ~600 lines long and the parser.mly file is ~1000 files long) so it's hard to pin down where I'm going wrong. I suspect I have a type error somewhere along the way.
Snippets of Code
Here's what my ast.atd file looks like:
...
type star = [ Star ]
type equal = [ Equal ]
type augassign = [
| Plusequal
| Minequal
| Starequal
| Slashequal
| Percentequal
| Amperequal
| Vbarequal
| Circumflexequal
| Leftshiftequal
| Rightshiftequal
| Doublestarequal
| Doubleslashequal
]
...
Here's what my parser.mly file looks like:
...
and_expr // Used in: xor_expr, and_expr
: shift_expr
{ $1 }
| and_expr AMPERSAND shift_expr
{ `And_shift ($1, `Ampersand, $3) } ;
shift_expr // Used in: and_expr, shift_expr
: arith_expr
{ $1 }
| shift_expr pick_LEFTSHIFT_RIGHTSHIFT arith_expr
{ `Shift_pick_arith ($1, $2, $3) } ;
pick_LEFTSHIFT_RIGHTSHIFT // Used in: shift_expr
: LEFTSHIFT
{ `Leftshift }
| RIGHTSHIFT
{ `Rightshift } ;
...
The error I get when I try to compile the files with
ocamlbuild -use-menhir -tag thread -use-ocamlfind -quiet -pkgs
'core,yojson,atdgen' main.native
is a type error, i.e.
This expression has type [GIANT TYPE CONSTRUCTION] but an expression
was expected of type [DIFFERENT GIANT TYPE CONSTRUCTION]
I realise that this question is somewhat difficult to answer in the abstract like this, and I'm happy to provide a link to the dropbox of my code, but I'd really appreciate if anyone could point me in the right direction.
Possibly of interest: I have some productions in parser.mly that were initially "empty" which I dealt with by using the ocaml option type (Some and None). Perhaps I could be having issues here?
About examples of code using menhir, you can have a look at the list on the right on the OPAM menhir page - all those depend on menhir.
I am looking to add a line(s) into a PowerShell script. I want to get my script to check if something exists in a CSV it will give a true/false response.
Basically I have a script to remove user access from O365 and AD including and mailbox changes.
My company also uses a lot of external portals which need removing manually.
I would like it to check the $EmailAddress input against CSV's (i.e DomainHostAccess.CSV, WebsiteHostAccess.CSV, SupplierAccess.Csv) then if there name is in the list it will output something similar to;
DomainHostAccess | True
WebsiteHostAccess | False
SupplierAccess | True
that way we know that we need to manually log into these services and remove accounts.
I have looked on the posts on here already and couldn't find anything suitable, I am fairly new to PS and this is a little advance for me so I would appreciate any help that can be given.
You could try something like this
$UserList = Import-Csv -Path C:\EmailAddress.CSV
$UserData = Import-Csv -Path C:\DomainHostAccess.CSV
ForEach ($Email in $UserList)
{
$userMatch = $UserData | where {$_.Name -like $Email.Name}
If($userMatch)
{
Write-host $Email.Name "Domain Access True"
}
Else
{
Write-host $Email.Name "Domain Access False"
}
}
You would need to have headers on your CSVs for this check to work.