1

I have a very simplistic 2 scripts and I'm trying to call the powershell script from another powershell run script

run script (run.ps1)

.\NewRG.ps1 -rgName "singleVM12" -location "Canada Central" -tags @{dept="Marketing"}

called script (newRG.ps1)

[CmdletBinding()]
param (
    [string]$rgName = "Test1-rg",
    [string]$location = "Canada Central",
    [Parameter(Mandatory)]
    [hashtable]$tags)
$newRG = New-AzResourceGroup -name $rgName -location $location -tags @{dept="marketing"} 
write-output "test"

I would expect that I should get test in the console but I get the properties of the Resource group

ResourceGroupName : singleVM12
Location : canadacentral
ProvisioningState : Succeeded

The issue is I have more complex scripts with multiple write-host entries that I want shown but none of it appears when I run the "run.ps1" file, it works fine if I just call the called script by itself. I tried using write-output and same thing happens. I noticed that hello world works, so I'm guessing something about the Azure commandlets are maybe causing this. Any way around this?

tmitch87
  • 11
  • 2

1 Answers1

0

I am using Write-Output to print the values in prompt. I have followed the same way you did.

Follow the workaround:

testout.ps1

# I am getting resource information 
[CmdletBinding()]
param (
    [string]$rgName = "test")


#$newRG = New-AzResourceGroup -name $rgName -location $location -tags @{dept="marketing"} 

$getResource = Get-AzResource -ResourceGroupName $rgName  
write-output "azure resoure get successfully- " $rgName


$getResource = Get-AzResource -ResourceGroupName $rgName
write-output "test2- " $rgName


$getResource = Get-AzResource -ResourceGroupName $rgName 
write-output "test3 - " $rgName


$getResource = Get-AzResource
write-output "test4- " $rgName
# you can use return to send the the required data to promt as well. But you can use end of your script otherwise it will skip after the return statement.
return $getResource.ResourceGroupName

test2.ps1

Calling testout.ps1 in test2.ps1 script.

# Connect Azure Account using specific Subscription Id if you are using more subscription in single account
Connect-AzAccount -SubscriptionId '<Your subscription Id>'
# calling test.ps1 script
.\testout.ps1 -rgName "<Your Resourcegroup Name>"

Result

enter image description here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15