I have deployed Azure Database for MySQL server and I want to add a firewall rule to it. I need to do it from PowerShell because this step is part of the greater solution. I tried the following code:
$resource = Get-AzureRmResource -ResourceGroupName $ResourceGroup.Variables.ResourceGroup `
-ResourceType "Microsoft.DBforMySQL/servers" -ResourceName $MySQLServer.ResourceName
$props = $resource.Properties
$props | Add-Member #{ipV4FirewallSettings = [ordered] #{ "firewallRules" = #() } }
$props.ipV4FirewallSettings.firewallRules = $MySQLServer.FirewallRules
$props | Add-Member #{administratorLoginPassword = "Qwerty123!" }
Set-AzureRmResource -PropertyObject $props -ResourceGroupName $ResourceGroup.Variables.ResourceGroup `
-ResourceType "Microsoft.DBforMySQL/servers" -ResourceName $MySQLServer.ResourceName -Force
Where $MySQLServer.FirewallRules are from json file in the following format:
"FirewallRules" : [
{ "firewallRuleName" : "test", "rangeStart": "0.0.0.0", "rangeEnd": "0.0.0.0" },
{ "firewallRuleName" : "test2", "rangeStart": "0.0.0.1", "rangeEnd": "255.255.255.255" }
],
This code does not throw any error, but it's not adding rules to the resource.
I need a pointer to where I made a mistake or some documentation, how to handle such task properly.
You can use New-AzureRmResource command to add Mysql firewall rules:
PS C:\Users\jason> $b = New-Object Psobject -Property #{startIpAddress="172.0.0.1" ; endIpAddress="172.0.0.8"}
PS C:\Users\jason> $b
startIpAddress endIpAddress
-------------- ------------
172.0.0.1 172.0.0.8
PS C:\Users\jason> New-AzureRmResource -ResourceId "/subscriptions/b83c1ed3-xxxx-xxxx-xxxx-2b83a074c23f/resourceGroups/jasonmysql/providers/Microsoft.DBforMySQL/servers/jasonmysql/firewallRules/rule2" -Properties $b -ApiVer
sion 2017-04-30-preview -Force
Name : rule2
ResourceId : /subscriptions/b83c1ed3-xxxx-xxxx-xxxx-2b83a074c23f/resourceGroups/jasonmysql/providers/Microsoft.DBforMySQL/servers/jasonmysql/firewallRules/rule2
ResourceName : jasonmysql/rule2
ResourceType : Microsoft.DBforMySQL/servers/firewallRules
ResourceGroupName : jasonmysql
SubscriptionId : b83c1ed3-xxxx-xxxx-xxxx-2b83a074c23f
Properties : #{startIpAddress=172.0.0.1; endIpAddress=172.0.0.8}
Related
I would like to pass Json data to Powershell script.
PowerShell script:
Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Out-File "file.json"
New-AzMySqlFirewallRule -Name “” -ResourceGroupName "dev" -ServerName "dev-core" -EndIPAddress "" -StartIPAddress ""
In the above powershell script I need to get values to "" from Json file mentioned below.So how to get Json parameter values during run time and all 3 parameters should be passed to the above command and so that it will create new firewall rule to new DB server.
Also, when I run the powershell command (Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Out-File "file.json") I am getting my Json file data in the below format.Not sure whether this format looks good but I need the below values start from pdbr_home,1.2.3.4 and 5.6.7.8 and similarly another 2 rows of data should be passed to my powershell command here New-AzMySqlFirewallRule -Name “” -ResourceGroupName "dev" -ServerName "dev-core" -EndIPAddress "" -StartIPAddress "". via for loop.
file.Json:
[
{
"EndIPAddress": "1.3.2.2",
"Id": "/subscriptions/abcdefg/resourceGroups/dev/providers/Microsoft.DBforMySQL/servers/db-dev- vm/firewallRules/praveen_Home",
"Name": "praveen_Home",
"StartIPAddress": "4.3.1.2",
"Type": "Microsoft.DBforMySQL/servers/firewallRules"
},
{
"EndIPAddress": "2.4.5.6",
"Id": "/subscriptions/abcdefg/resourceGroups/dev/providers/Microsoft.DBforMySQL/servers/db-dev- vm/firewallRules/pdbr_Home",
"Name": "pdbr_Home",
"StartIPAddress": "3.2.1.2",
"Type": "Microsoft.DBforMySQL/servers/firewallRules"
}
]
The below command output as follows.
PS /home/praveen> Get-Command json
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet ConvertFrom-Json 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet ConvertTo-Json 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Test-Json 7.0.0.0 Microsoft.PowerShell.Utility
Application json_pp 0.0.0.0 /usr/bin/json_pp
Application json_pp 0.0.0.0 /bin/json_pp
Error:
Error:
New-AzMySqlFirewallRule: /home/praveen/dbtest.ps1:21
Line |
21 | … -ServerName "praveen-dev" -EndIPAddress $entry.EndIPAddress -StartI …
| ~~~~~~~~~~~~~~~~~~~
| Cannot bind argument to parameter 'EndIPAddress' because it is an empty string.
Final solution worked for me now:
##################### Updating Firewall rules from Soiurce DB server to Target DB server ##################
Write-Host -NoNewline "Updating Firewall rules from Soiurce DB server to Target DB server"
Get-AzMySqlFirewallRule -ResourceGroupName $ResourceGroupName -ServerName $SourceDBServerName | Select-Object Name, StartIPaddress, EndIPaddress | Convertto-Json | Out-File "firewallrule.json"
foreach ($frule in (Get-Content firewallrule.json -raw | ConvertFrom-Json)) {
New-AzMySqlFirewallRule -Name $frule.Name -ResourceGroupName $ResourceGroupName -ServerName $TargetDBServerName -EndIPAddress $frule.EndIPAddress -StartIPAddress $frule.StartIPAddress
}
Use Convertto-Json before writing file.
Docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.2
Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Convertto-Json | Out-File "file.json"
EDIT as requested:
Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Select-Object Name,"StartIP address", "EndIP address"| Convertto-Json | Out-File "file.json"
Another EDIT as requested (fixed my mistake - thank you #sage pourpre):
foreach ($entry in (Get-Content file.json -raw | ConvertFrom-Json) {
New-AzMySqlFirewallRule -name $entry.Name `
-ResourceGroupName "dev" `
-ServerName "dev-core" `
-StartIPAddress $entry.StartIPAddress `
-EndIPAddress $entry.EndIPAddress
}
try the below:
Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Convertto-Json | Out-File "file.json"
$data = Get-Content "C:\Users\me\file.json" | Out-String | ConvertFrom-Json #replace path to where you have exported the json file
foreach ($line in $data) {
New-AzMySqlFirewallRule -name $line.Name `
-ResourceGroupName "dev" `
-ServerName "dev-core" `
-StartIPAddress $line.StartIPAddress `
-EndIPAddress $line.EndIPAddress
}
A different method, although the question is for JSON, would be to just store the rules in a variable.
$rules = Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server"
foreach ($rule in $rules){
New-AzMySqlFirewallRule -name $rule.Name `
-ResourceGroupName "dev" `
-ServerName "dev-core" `
-StartIPAddress $rule.StartIPAddress `
-EndIPAddress $rule.EndIPAddress
}
I was able to fix this solution with help of all the above inputs few days ago. Thanks to all.Really appreciated your help.
Updating Firewall rules from Source DB server to Target DB server
Write-Host -NoNewline "Updating Firewall rules from Soiurce DB server to Target DB server"
Get-AzMySqlFirewallRule -ResourceGroupName $ResourceGroupName -ServerName $SourceDBServerName | Select-Object Name, StartIPaddress, EndIPaddress | Convertto-Json | Out-File "firewallrule.json"
foreach ($frule in (Get-Content firewallrule.json -raw | ConvertFrom-Json)) {
New-AzMySqlFirewallRule -Name $frule.Name -ResourceGroupName $ResourceGroupName -ServerName $TargetDBServerName -EndIPAddress $frule.EndIPAddress -StartIPAddress $frule.StartIPAddress
}
//role-definition.json
{
"RoleName": "MyReadWriteRole",
"Type": "CustomRole",
"AssignableScopes": ["/"],
"Permissions": [{
"DataActions": [
"Microsoft.DocumentDB/databaseAccounts/readMetadata",
"Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*",
"Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/*"
]
}]
}
Coverting it into json object as i need to pass it as an argument in a powershell parameter.
So i did below -
Created a powershell parameter
$roleDef= #"
{
"RoleName": "MyReadWriteRole",
"Type": "CustomRole",
"AssignableScopes": ["/"],
"Permissions": [{
"DataActions": [
"Microsoft.DocumentDB/databaseAccounts/readMetadata",
"Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/",
"Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/"
]
}]
}
"#
$jsonObject = $roleDef | ConvertFrom-Json
az cosmosdb sql role definition create --account-name cosmosdbaccname --resource-group 'my-rg' --body $jsonObject
but I get below error -
Failed to parse string as JSON:
#{RoleName=MyReadWriteRole; Type=CustomRole; AssignableScopes=System.Object[]; Permissions=System.Object[]}
Error detail: Expecting value: line 1 column 1 (char 0)
So I'm trying to create a "download" function that uses a piped object property to determine a download method (sftp or http). Then either create an sftp script for putty/winscp or curl the http url. I am defining objects as follows:
#WinSCP
$winscp = new-object psobject
$winscp | add-member noteproperty name "WinSCP"
$winscp | add-member noteproperty dltype "http"
$winscp | add-member noteproperty file "winscp.exe"
$winscp | add-member noteproperty url "https://cdn.winscp.net/files/WinSCP-5.17.8-Setup.exe"
$winscp | add-member noteproperty path "$env:ProgramFiles(x86)\WinSCP"
$winscp | add-member noteproperty install 'msiexec /i "$DataPath\$winscp.file" /quiet /norestart'
#Database
$db = new-object psobject
$db | add-member noteproperty name "Client Database"
$db | add-member noteproperty dltype "sftp"
$db | add-member noteproperty file "database_"
$db | add-member noteproperty ver "check"
$db | add-member noteproperty ext ".csv"
$db | add-member noteproperty dir "db"
#DatabaseVersion
$db_ver = new-object psobject
$db_ver | add-member noteproperty name "Database Version File"
$db_ver | add-member noteproperty dltype "sftp"
$db_ver | add-member noteproperty file "current_version.txt"
$db_ver | add-member noteproperty dir "db"
Currently I'm having issues with the $Input variable within the function. It can only be used once and does not translate into an if statement. Since it contains an object with multiple properties, it needs converted to a new object within the function first I think. I'm new to powershell and haven't found a way of doing this yet. Here is the function I made and am trying to use:
function Download () {
#HTTP Download Method
if ($input.dltype -eq "http") {
curl $input.url -O $DataPath\$input.file
#HTTP Success or Error
$curlResult = $LastExitCode
if ($curlResult -eq 0)
{
Write-Host "Successfully downloaded $input.name"
}
else
{
Write-Host "Error downloading $input.name"
}
pause
}
#SFTP Download Method
if ($input.dltype -eq "sftp") {
sftpPassCheck
#Detect if version required
if ($input.ver = "check") {
#Download the objects version file
"$+$Input+_ver" | Download
#Update the object's ver property
$input.ver = [IO.File]::ReadAllText("$DataPath\current_version.txt")
#Build the new filename
$input.file = "$input.file"+"$input.ver"+"$input.ext"
#Delete the version file
Remove-Item "$DataPath\current_version.txt"
}
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/log="$DataPath\SFTP.log" /ini=nul `
/command `
"open sftp://ftpconnector:$script:sftp_pass#$input.ip/ -hostkey=`"`"ssh-ed25519 255 SETvoRlAT0/eJJpRhRRpBO5vLfrhm5L1mRrMkOiPS70=`"`" -rawsettings ProxyPort=0" `
"cd /$input.dir" `
"lcd $DataPath" `
"get $input.file" `
"exit"
#SFTP Success or Error
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host "Successfully downloaded $input.name"
}
else
{
Write-Host "Error downloading $input.name"
}
}
}
I'm probably missing something simple but I'm clueless at this point. Oh usage should be:
WinSCP | download
The proper way to bind input from the pipeline to a function's parameters is to declare an advanced function - see about_Functions_Advanced_Parameters and the implementation in the bottom section of this answer.
However, in simple cases a filter will do, which is a simplified form of a function that implicitly binds pipeline input to the automatic $_ variable and is called for each input object:
filter Download {
if ($_.dltype -eq "http") {
# ...
}
}
$input is another automatic variable, which in simple (non-advanced) functions is an enumerator for all pipeline input being received and must therefore be looped over.
That is, the following simple function is the equivalent of the above filter:
function Download {
# Explicit looping over $input is required.
foreach ($obj in $input) {
if ($obj.dltype -eq "http") {
# ...
}
}
}
If you do want to turn this into an advanced function (note that I've changed the name to conform to PowerShell's verb-noun naming convention):
function Invoke-Download {
param(
# Declare a parameter explicitly and mark it as
# as pipeline-binding.
[Parameter(ValueFromPipeline, Mandatory)]
$InputObject # Not type-constraining the parameter implies [object]
)
# The `process` block is called for each pipeline input object
# with $InputObject referencing the object at hand.
process {
if ($InputObject.dltype -eq "http") {
# ...
}
}
}
mklement0 is spot on - $input is not really meant to used directly, and you're probably much better off explicitly declaring your input parameters!
In addition to the $InputObject pattern shown in that answer, you can also bind input object property values to parameters by name:
function Download
{
param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[Alias('dltype')]
[string]$Protocol = 'http'
)
process {
Write-Host "Choice of protocol: $Protocol"
}
}
Notice that although the name of this parameter is $Protocol, the [Alias('dltype')] attribute will ensure that the value of the dltype property on the input object is bound.
The effect of this is:
PS ~> $WinSCP,$db |Download
Choice of protocol: http
Choice of protocol: sftp
Keep repeating this pattern for any required input parameter - declare a named parameter mapped to property names (if necessary), and you might end up with something like:
function Download
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateSet('sftp', 'http')]
[Alias('dltype')]
[string]$Protocol,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[Alias('dir')]
[string]$Path = $PWD,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('url','file')]
[string]$Uri
)
process {
Write-Host "Downloading $Uri to $Path over $Protocol"
}
}
Now you can do:
PS ~> $WinSCP,$db |Download
Downloading https://cdn.winscp.net/files/WinSCP-5.17.8-Setup.exe to C:\Program Files(x86)\WinSCP over http
Downloading database_ to db over sftp
We're no longer dependent on direct access to $input, $InputObject or $_, nice and clean.
Please see the about_Functions_Advanced_Parameters help file for more information about parameter declaration.
In Azure CLI, there is az functionapp, but no such equivalent can be found in Powershell AzureRM-library nor Az-library.
Using raw Azure resources, I've attempted something like this to create a function app on my Application Service Plan:
New-AzResource -ResourceType 'Microsoft.Web/Sites' `
-ResourceGroupName "MyRgName" `
-Location "westeurope" `
-ResourceName "MyFunctionName" `
-kind 'functionapp' `
-Properties #{ServerFarmId="abc-123"; alwaysOn=$True;} `
-ApiVersion '2018-11-01' `
-Force;
It almost works, but doesn't create a 100% working Function App. Azure Portal will spit lots of errors and warnings, for example from missing Host Keys.
Alternatives:
ARM-templates. What to put into a template to successfully create Azure Function? I have no idea. The one generated by Azure Portal is useless.
Azure Portal: Not really handy approach for environment setup from Azure DevOps release pipeline, but it will create a fully working Function App.
The question is: How to create a Function App from a Powershell script?
I am doing the exact same thing to create a dev sandbox environment.
Provisioning function apps is a gap in the Az Powershell module but it does appear to be possible.
I provisioned my function app by following the steps here https://clouddeveloper.space/2017/10/26/deploy-azure-function-using-powershell/ but changed it to use an existing app service plan instead of consumption plan.
$AppServicePlan = "abc-123"
$AppInsightsKey = "your key here"
$ResourceGroup = "MyRgName"
$Location = "westeurope"
$FunctionAppName = "MyFunctionName"
$AzFunctionAppStorageAccountName = "MyFunctionAppStorageAccountName"
$FunctionAppSettings = #{
ServerFarmId="/subscriptions/<GUID>/resourceGroups/$ResourceGroup/providers/Microsoft.Web/serverfarms/$AppServicePlan";
alwaysOn=$True;
}
# Provision the function app service
New-AzResource -ResourceGroupName $ResourceGroup -Location $Location -ResourceName $FunctionAppName -ResourceType "microsoft.web/sites" -Kind "functionapp" -Properties $FunctionAppSettings -Force | Out-Null
$AzFunctionAppStorageAccountKey = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroup -AccountName $AzFunctionAppStorageAccountName | Where-Object { $_.KeyName -eq "Key1" } | Select-Object Value
$AzFunctionAppStorageAccountConnectionString = "DefaultEndpointsProtocol=https;AccountName=$AzFunctionAppStorageAccountName;AccountKey=$($AzFunctionAppStorageAccountKey.Value)"
$AzFunctionAppSettings = #{
APPINSIGHTS_INSTRUMENTATIONKEY = $AppInsightsKey;
AzureWebJobsDashboard = $AzFunctionAppStorageAccountConnectionString;
AzureWebJobsStorage = $AzFunctionAppStorageAccountConnectionString;
FUNCTIONS_EXTENSION_VERSION = "~2";
FUNCTIONS_WORKER_RUNTIME = "dotnet";
}
# Set the correct application settings on the function app
Set-AzWebApp -Name $FunctionAppName -ResourceGroupName $ResourceGroup -AppSettings $AzFunctionAppSettings | Out-Null
This might help:
To create an Azure Function we have dependency over "Storage Account", "Service Plan", "a resource group" and "Application Insight"(optional). Below i am initially defining variables. Post that i am checking if Resource group exist. If not it will create a new one. Post which i created Azure Storage Account, Service Plan and Application Insight. In Azure function we need to select Runtime Stack which can be "Java"/"DotNet"/"Python" etc. Here I am using "Dotnet". Azure Function requires Storage Account keys to link the same, which is extracted below under variables "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING" etc. To link AppInsight with Function we need to map Application_Insight_InstrumentationKey. Please follow inline comments :
#=============Defining All Variables=========
$location = 'Southeast Asia'
$resourceGroupName = 'functionrgnew1'
$storageAccount = 'functionsasdnewqq1'
$subscriptionId = '<id>'
$functionAppName = 'functionapppsdfsdnew1'
$appInsightsName = 'appinsightnameprdad'
$appServicePlanName = 'functionappplan'
$tier = 'Premium'
#========Creating Azure Resource Group========
$resourceGroup = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $resourceGroupName }
if ($resourceGroup -eq $null)
{
New-AzResourceGroup -Name $resourceGroupName -Location $location -force
}
#selecting default azure subscription by name
Select-AzSubscription -SubscriptionID $subscriptionId
Set-AzContext $subscriptionId
#========Creating Azure Storage Account========
if(!(Test-AzureName -Storage $storageAccount))
{
New-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageAccount -Location $location -SkuName "Standard_LRS"
}
#========Creating App Service Plan============
New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $appServicePlanName -Location $location -Tier $tier
$functionAppSettings = #{
ServerFarmId="/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/serverfarms/$appServicePlanName";
alwaysOn=$True;
}
#========Creating Azure Function========
$functionAppResource = Get-AzResource | Where-Object { $_.ResourceName -eq $functionAppName -And $_.ResourceType -eq "Microsoft.Web/Sites" }
if ($functionAppResource -eq $null)
{
New-AzResource -ResourceType 'Microsoft.Web/Sites' -ResourceName $functionAppName -kind 'functionapp' -Location $location -ResourceGroupName $resourceGroupName -Properties $functionAppSettings -force
}
#========Creating AppInsight Resource========
New-AzApplicationInsights -ResourceGroupName $resourceGroupName -Name $appInsightsName -Location $location
$resource = Get-AzResource -Name $appInsightsName -ResourceType "Microsoft.Insights/components"
$details = Get-AzResource -ResourceId $resource.ResourceId
$appInsightsKey = $details.Properties.InstrumentationKey
#========Retrieving Keys========
$keys = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $storageAccount
$accountKey = $keys | Where-Object { $_.KeyName -eq 'Key1' } | Select-Object -ExpandProperty Value
$storageAccountConnectionString = 'DefaultEndpointsProtocol=https;AccountName='+$storageAccount+';AccountKey='+$accountKey
#========Defining Azure Function Settings========
$AppSettings =#{}
$AppSettings =#{'APPINSIGHTS_INSTRUMENTATIONKEY' = $appInsightsKey;
'AzureWebJobsDashboard' = $storageAccountConnectionString;
'AzureWebJobsStorage' = $storageAccountConnectionString;
'FUNCTIONS_EXTENSION_VERSION' = '~2';
'FUNCTIONS_WORKER_RUNTIME' = 'dotnet';
'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING' = $storageAccountConnectionString;
'WEBSITE_CONTENTSHARE' = $storageAccount;}
Set-AzWebApp -Name $functionAppName -ResourceGroupName $resourceGroupName -AppSettings $AppSettings
The best way to do this from PowerShell is to use an ARM template, rather than try to create each resource individually. You can find an example template here. It also hooks up the app to github, but you can leave out that part if you just want an empty app
I've written one JSON script with nested arrays and trying to display the all the data based on title. but unfortunately i couldn't make that happen. i'm placing my JSON and powershell code below. kindly help me.
JSON code:
{
"Deploy": {
"ENV": "DIT",
"Servers": [
{
"Webservers": [
{
"Server": ["Server1","Server2"],
"site": ["website1","WebSite2"],
"VDIR": ["WebSite1","WebSite2"]
},
{
"Server": ["Server1","Server2"],
"site": ["WebSite1","WebSite2"],
"VDIR": ["WebSite1","WebSite2"]
}
]
},
{
"Winservers": [
{
"Server": ["Server1","Server2"],
"winService": ["service1","service2"]
},
{
"Server": ["Server3","Server4"],
"winService": ["service3","service4"]
}
]
}
]
}
}
Powershell Code:
$Deploy=(Get-Content D:\Deploy.json -Raw) -join "`n"|ConvertFrom-Json
foreach($i in $Deploy.Deploy.Servers) {
$s=($i | Get-Member -MemberType *property).Name
Write-host "$s" -ForegroundColor Yellow
foreach($j in $s) {
$items=($i.$j|Get-Member -MemberType *property).Name
Write-host "$items" -ForegroundColor Yellow
foreach($k in $items) {
$z=($j.$k | Get-Member -MemberType *property).Name
Write-host "$z" -ForegroundColor Yellow
}
}
}
Current result is :
Webservers
Server site VDIR
Winservers
Server winService
But expected result should be like below:
Webservers:
Server: Sites: VDIR:
list of servers list of sites list of VDIR
Server: Sites: VDIR:
list of servers list of sites list of VDIR
Winservers:
Server: services:
list of servers list of sites
Kindly help ... i'm trying by best in different ways but none of one gave expected result.
OK. I think your problem can be solved with next way.
$ht, $src = #{}, 'D:\sandbox\source.xml'
($srv = (Get-Content $src | ConvertFrom-Json).Deploy.Servers).ForEach{
$name = $_.PSObject.Properties.Name
$ht[$name] = $srv.$name
}
Write-Host Webservers -ForegroundColor Yellow
$ht.Webservers | Out-String
Write-Host Winservers -ForegroundColor Yellow
$ht.Winservers | Out-String
If what you want to achieve is just a formatted output, maybe this is more to your liking
$deploy = Get-Content 'D:\Deploy.json' -Raw | ConvertFrom-Json
Write-Host "Webservers:`r`n-----------"
$items = #()
foreach ($server in $deploy.Deploy.Servers.Webservers) {
foreach ($item in $server) {
$items += New-Object -TypeName PSObject -Property ([ordered]#{
Server = #($item.Server) -join ', '
Sites = #($item.site) -join ', '
VDIR = #($item.VDIR) -join ', '
})
}
}
$items | Out-String
Write-Host "Winservers:`r`n-----------"
$items = #()
foreach ($server in $deploy.Deploy.Servers.Winservers) {
foreach ($item in $server) {
$items += New-Object -TypeName PSObject -Property ([ordered]#{
Server = #($item.Server) -join ', '
WinService = #($item.winService) -join ', '
})
}
}
$items | Out-String
The output will look like this:
Webservers:
-----------
Server Sites VDIR
------ ----- ----
Server1, Server2 website1, WebSite2 WebSite1, WebSite2
Server1, Server2 WebSite1, WebSite2 WebSite1, WebSite2
Winservers:
-----------
Server WinService
------ ----------
Server1, Server2 service1, service2
Server3, Server4 service3, service4
A pragmatic for-display-only solution that doesn't output the exact format you describe, but is concise (PSv3+):
(Get-Content -Raw D:\Deploy.json | ConvertFrom-Json).Deploy.Servers |
ForEach-Object {
$serverTypePropName = $_.psobject.properties.Name
Write-Host "${serverTypePropName}:"
$_.$serverTypePropName | Format-Table
}
(Get-Content -Raw D:\Deploy.json | ConvertFrom-Json) converts the JSON input to custom objects.
.Deploy.Server accesses the array-valued property whose elements are the objects whose Webservers and Winservers properties, respectively, contain the server-details sub-arrays.
In the ForEach-Object script block:
$_.psobject.properties.Name extracts the name of the one property (Webservers or Winservers) of each input object that contains the array of server details.
$_.$serverTypePropName | Format-Table accesses that property and renders the server-details array as a table.
Note that explicit use of Format-Table is needed, because the server-detail arrays have different property structures between the Webservers and the Winservers object; without an explicit formatting/output command, only the first array's properties would render.
With your sample input, the above yields:
Webservers:
Server site VDIR
------ ---- ----
{Server1, Server2} {website1, WebSite2} {WebSite1, WebSite2}
{Server1, Server2} {WebSite1, WebSite2} {WebSite1, WebSite2}
Winservers:
Server winService
------ ----------
{Server1, Server2} {service1, service2}
{Server3, Server4} {service3, service4}
I'm writing a function and observing some unusual behavior with the output.
This is the code:
Function Get-CompInfo
{
[Cmdletbinding()]
PARAM
(
[Parameter(ValueFromPipeline=$true)]
[Alias('Comp', 'Host')]
[string[]]$computername
)
Begin
{
}
Process
{
if ($computername -eq $Null) {
$computername=$env:computername
$VerboseOut="No computer specified. Running against local computer $computername :"
}
Else {
$VerboseOut="Getting information for computer $computername :"
}
Write-Verbose $VerboseOut
$CompInfo=Get-WmiObject Win32_Computersystem -computername $computername
$OSInfo=Get-WmiObject Win32_OperatingSystem -computername $computername
$Properties = [ordered]#{
'Input'=$computername
'SystemName'=$CompInfo.Name
'Manufacturer'=$CompInfo.Manufacturer
'Model'=$CompInfo.Model
'PhysicalMemory'=$CompInfo.TotalPhysicalMemory
'LogicalProcessors'=$CompInfo.NumberOfLogicalProcessors
'OSCaption'=$OSInfo.Caption
'OSArchitecture'=$OSInfo.OSArchitecture
'ServicePackMajorVersion'=$OSInfo.ServicePackMajorVersion}
# Output Information
$obj=New-Object -TypeName PSobject -Property $Properties
write-output $obj
}
End
{
}
}
When passing parameters from the pipline:
"karuma", "localhost" | Get-CompInfo
Input : {karuma}
SystemName : KARUMA
Manufacturer : Hewlett-Packard
Model : h9-1400a
PhysicalMemory : 17115000832
LogicalProcessors : 8
OSCaption : Microsoft Windows 10 Pro
OSArchitecture : 64-bit
ServicePackMajorVersion : 0
Input : {localhost}
SystemName : KARUMA
Manufacturer : Hewlett-Packard
Model : h9-1400a
PhysicalMemory : 17115000832
LogicalProcessors : 8
OSCaption : Microsoft Windows 10 Pro
OSArchitecture : 64-bit
ServicePackMajorVersion : 0
I get the same kind of output when I pass a text file with a list of computer names.
When specifying multiple host names something different:
Get-CompInfo -computername localhost, karuma
Input : {localhost, karuma}
SystemName : {KARUMA, KARUMA}
Manufacturer : {Hewlett-Packard, Hewlett-Packard}
Model : {h9-1400a, h9-1400a}
PhysicalMemory : {17115000832, 17115000832}
LogicalProcessors : {8, 8}
OSCaption : {Microsoft Windows 10 Pro, Microsoft Windows 10 Pro}
OSArchitecture : {64-bit, 64-bit}
ServicePackMajorVersion : {0, 0}
I'm expecting to see table output when passing multiple values as would by seen by piping to format-table.
Any help on what I need to change to get the output as desired would be appreciated.
It's the way you have your function setup... when using the pipeline the PROCESS block will be run once for each object in the pipeline - the $computername param will only contain a single object at a time in this situation.
When you specify two computers in $computername param, you are changing the way the entire function is running as it contains two objects.
It's easy to fix by wrapping your function in a Foreach like so:
Function Get-CompInfo
{
[Cmdletbinding()]
PARAM
(
[Parameter(ValueFromPipeline=$true)]
[Alias('Comp', 'Host')]
[string[]]$computername
)
Begin {}
Process
{
Foreach ($computer in $computername) {
if ($computer -eq $Null) {
$computer=$env:computername
$VerboseOut="No computer specified. Running against local computer $computer :"
}
Else {
$VerboseOut="Getting information for computer $computer :"
}
Write-Verbose $VerboseOut
$CompInfo=Get-WmiObject Win32_Computersystem -computername $computer
$OSInfo=Get-WmiObject Win32_OperatingSystem -computername $computer
$Properties = [ordered]#{
'Input'=$computer
'SystemName'=$CompInfo.Name
'Manufacturer'=$CompInfo.Manufacturer
'Model'=$CompInfo.Model
'PhysicalMemory'=$CompInfo.TotalPhysicalMemory
'LogicalProcessors'=$CompInfo.NumberOfLogicalProcessors
'OSCaption'=$OSInfo.Caption
'OSArchitecture'=$OSInfo.OSArchitecture
'ServicePackMajorVersion'=$OSInfo.ServicePackMajorVersion}
# Output Information
$obj=New-Object -TypeName PSobject -Property $Properties
write-output $obj
}
}
End {}
}