Wednesday, September 16, 2020

#100: How to execute a string in Powershell?

 Rarely, but sometimes we need to run the command from an expression that we create. I stumbled upon a situation where I was to read the content of a config file and run it. 

There could be other ways to do it, but invoke-expression can be used with simplicity. 


Invoke-Command

SYNTAX

    Invoke-Command [-ScriptBlock] <scriptblock> [-NoNewScope] [-InputObject <psobject>] [-ArgumentList <Object[]>]

    [<CommonParameters>]


EXAMPLE: 

Read content of a file that has EXPRESSION=get-help ls 

$str=gc "c:\temp\cmd_list.txt" | where { $_ -match "EXPRESSION=" } | foreach { $_.split("=")[1] 

invoke-command $str

      

      

I hope it was quite easy and simple. 

Happy scripting !!!

1 comment:

  1. Инстраграмм являться самой популярной площадкой для продвижения собственного бизнеса. Но, как показывает практика, люди гораздо чаще подписываются на профили в которых уже достаточное количество подписчиков. В случае если заниматься продвижение своими силами, потратить на это можно очень множество времени, потому гораздо лучше обратиться к специалистам из Krutiminst.ru тут https://repo.getmonero.org/krutiminst4

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...