Powershell functions executed regardless of being called - function

I'm having trouble understanding how Powershell treats functions. In the following script all functions are called, even if I never actually call the main function. Does powershell not have a concept of call chain?
param([string]$directory)
[string]$global:sqlscript;
$global:types = #{
"double" = "DOUBLE PRECISION";
"int" = "INTEGER";
"QString" = "INTEGER";
"Ignored" = "1";
"Normal" = "2";
"Critical" = "3" }
function resultToSql($element)
{
$global:sqlscript += ('"')
$global:sqlscript += ($element.name + '" ')
$global:sqlscript += ($global:types.Get_Item($element.type))
$global:sqlscript += (',' + [Environment]::Newline)
$global:sqlscript += ('"' + $element.name + "_metric_group" + " " + $global:types.Get_Item($element.metric_group.type))
$global:sqlscript += (',' + [Environment]::Newline)
}
function xmlToSql($source)
{
Write-Host "Parsing...";
$global:sqlscript += "CREATE TABLE IF NOT EXISTS " + '"' + $source.spec.task.ToLower() + '"'+ [Environment]::NewLine
$global:sqlscript += '"' + "id" + '"' + " SERIAL NOT NULL" + [Environment]::NewLine
foreach ($node in $source.spec.measure) {
resultToSql $node
}
foreach ($m in $source.spec.error) {
resultToSql $m
}
$global:sqlscript += '"' + "weighted_sum" + '" ' + $global:types.Get_Item("double") + [Environment]::Newline;
}
function main
{
if ($directory -eq $null) { exit }
else
{
$xmlfiles = Get-ChildItem -Path $directory -include *Spec.xml
foreach ($xmlfile in $xmlfiles)
{
Write-Host "Filename:" $xmlfile;
[xml]$spec = Get-Content $file;
xmlToSql $spec;
Write-Host $script;
}
}
}

PowerShell cant magically detect changes to scripts, close the ISE and re-open it then run your script again. If that fails take the contents of your script paste it in the ISE and click the execute button, i just did that and main didnt run.

Unlike a C/C++/C# program, "you" need to call the Main function - at the bottom of this script. When you run the script above all it does is create the functions you've defined. It doesn't run any of them. You have to do that by calling them in the script and one of those calls has to be at the script level (outside any functions).

Remove the main function container so it resembles the code below:
if ($directory -eq $null) { exit }
else
{
$xmlfiles = Get-ChildItem -Path $directory -include *Spec.xml
foreach ($xmlfile in $xmlfiles)
{
Write-Host "Filename:" $xmlfile;
[xml]$spec = Get-Content $file;
xmlToSql $spec;
Write-Host $script;
}
}
Powershell doesn't execute from Main like C#/C++. It executes what statements are first received outside functions. In this case above it will execute the if statement first as it appears outside the function box.

Related

Powershell - JSON format to PAC file convert

I have used the following code to display JSON results but now need to change the script to display the output instead of side by side. I have tried a script like below, but just cant seem to get it to do what I want.
My question is :
I want to remove || before the last bracket. if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com") || ) As a result , it will be if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com"))
I need to change the script to display the my desired output instead of side by side.
Here is my script :
$result = Invoke-WebRequest "https://endpoints.office.com/endpoints/worldwide?noipv6&ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7"
$services = ConvertFrom-Json $result
$likeFilter = "12"
$services = $services | Where-Object { $_.id -like $likeFilter }
$urls = [System.Collections.ArrayList]#()
$services
function add_url($url){
if(!$urls.Contains($url)){ $urls.Add($url); }
}
foreach($service in $services){
foreach($url in $service.urls){ add_url($url);
}
}
# OUTPUT
$txt_proxypacText += "// This PAC file will provide proxy config to Microsoft 365 services`r`n"
$txt_proxypacText += "// using data from the public web service for all endpoints`r`n"
$txt_proxypacText += "function FindProxyForURL(url, host)`r`n"
$txt_proxypacText += "{`r`n"
$txt_proxypacText += "var direct = ""DIRECT"";`r`n"
$txt_proxypacText += "var proxyServer = ""PROXY 10.11.12.13:8080"";`r`n"
$txt_proxypacText += "host = host.toLowerCase();`r`n"
$txt_proxypacText += "if ("
foreach($url in $urls){
$txt_proxypacText += "shExpMatch(host, ""$url"") || "
}
$txt_proxypacText += ")`r`n"
$txt_proxypacText += "{`r`n"
$txt_proxypacText += "`r`n return direct;"
$txt_proxypacText += "`r`n}"
$txt_proxypacText += "`r`n return proxyServer;"
$txt_proxypacText += "`r`n}"
Output:
// This PAC file will provide proxy config to Microsoft 365 services
// using data from the public web service for all endpoints
function FindProxyForURL(url, host)
{
var direct = "DIRECT";
var proxyServer = "PROXY 10.11.12.13:8080";
host = host.toLowerCase();
if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com") || )
{
return direct;
}
return proxyServer;
}
My Desired Output :
// This PAC file will provide proxy config to Microsoft 365 services
// using data from the public web service for all endpoints
function FindProxyForURL(url, host)
{
var direct = "DIRECT";
var proxyServer = "PROXY 10.11.12.13:8080";
host = host.toLowerCase();
if(shExpMatch(host, "*.lync.com")
|| shExpMatch(host, "*.teams.microsoft.com")
|| shExpMatch(host, "teams.microsoft.com"))
{
return direct;
}
return proxyServer;
}
I would make use of a Here-String with a preformated set of shExpMatch(..) lines.
Using that also relieves you from doubling quotes and string concatenations using +=
# demo urls
$urls = "*.lync.com", "*.teams.microsoft.com", "teams.microsoft.com"
$hostMatches = $(for ($i = 0; $i -lt $urls.Count; $i++) {
$prefix = if ($i -eq 0) { '' } else { ' || '}
'{0}shExpMatch(host, "{1}")'-f $prefix, $urls[$i]
}) -join [Environment]::NewLine
$txt_proxypacText = #"
// This PAC file will provide proxy config to Microsoft 365 services
// using data from the public web service for all endpoints
function FindProxyForURL(url, host)
{
var direct = "DIRECT";
var proxyServer = "PROXY 10.11.12.13:8080";
host = host.toLowerCase();
if ($hostMatches)
{
return direct;
}
return proxyServer;
}
"#
$txt_proxypacText
Output:
// This PAC file will provide proxy config to Microsoft 365 services
// using data from the public web service for all endpoints
function FindProxyForURL(url, host)
{
var direct = "DIRECT";
var proxyServer = "PROXY 10.11.12.13:8080";
host = host.toLowerCase();
if (shExpMatch(host, "*.lync.com")
|| shExpMatch(host, "*.teams.microsoft.com")
|| shExpMatch(host, "teams.microsoft.com"))
{
return direct;
}
return proxyServer;
}
As requested, I think the top part of the code, where you are gathering the urls in an arraylist can be done much easier.
One note before: You are using a $likeFilter variable with a string "12".
In that case, you could probably better use the -eq operator instead of the -like operator that has more use for filtering with wildcards (i.e. "12*").
For now, I'm assuming you want to get only the service with id matching "12" exactly.
$url = "https://endpoints.office.com/endpoints/worldwide?noipv6&ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7"
$filter = 12
# get an array of urls from the service(s) that get through the filter
$urls = ((Invoke-WebRequest $url | ConvertFrom-Json) | Where-Object { $_.id -eq $filter }).urls | Select-Object -Unique
# EXAMPLE prepare begin
$urls = #(
'microsoft.com',
'*.microsoft.com',
'teams.microsoft.com',
'*.teams.microsoft.com')
# EXAMPLE prepare End
$urlLines = $urls |
ForEach-Object { return $_.Trim() } |
ForEach-Object {
if($_.StartsWith('*.')) {
return "shExpMatch(host, '$($_)')"
} else {
return "host == '$($_)'"
}}
$innerIf = [String]::Join("`r`n" + (' ' * 8) + "|| ", $urlLines)
#// $txt_proxypacText += " if ($($innerIf))"
Write-Host " if ($($innerIf))"
# Output:
# if (host == "microsoft.com"
# || shExpMatch(host, "*.microsoft.com")
# || host == "teams.microsoft.com"
# || shExpMatch(host, "*.teams.microsoft.com"))
I got this - simple counter method:
$counter = 0
foreach($url in $urls){
If ($counter -eq $urls.Count){
$txt_proxypacText += "shExpMatch(host, ""$url"") `r`n"
}else{
$txt_proxypacText += "shExpMatch(host, ""$url"") || `r`n"
}
$counter++
}
Probably needs some tidying up with the tab characters.

Iterating through values from CSV and concatenating them with timestamp

I'm trying to concatenate value imported from CSV with a timestamp (to create a filename).
It seems that when I try to use foreach with the $csv it loops through all values within one iteration.
$csv = Import-Csv .\servers.csv
function LogTime {return Get-Date -Format "yyyymmdd_HHmmss"}
foreach ($server in $csv)
{
$filename = $(LogTime) + "_" + $csv.SERVERNAME + ".log"
$filename
}
This results in.
20191024_091007_server1 server2.log
20191024_091007_server1 server2.log
What I'm looking for:
20191024_091007_server1.log
20191024_091007_server2.log
The foreach loop is not using the $server variable
$csv = Import-Csv .\servers.csv
function LogTime {return Get-Date -Format "yyyymmdd_HHmmss"}
foreach ($server in $csv)
{
$filename = $(LogTime) + "_" + $server.SERVERNAME + ".log"
$filename
}

Function returns value to console but not to file

Trying to write a function to create a new line to be added to a table for export. The following outputs the correct values to the console but the CSV is empty.
If I place the code to create $newline at various point in the script it works fine but not when I call it as a function.
$report = #()
Function CreateNewLine
{
$lineproperties = #{
Cluster = $cluster
Node = $node
Database = $d.Name
LogCount = $logcount
LogPath = $p
}
$newline = New-Object PSObject -property $lineproperties
}
# Loop to create values for $cluster etc...
CreateNewLine
$report += $newline
# End loop
$report | Export-CSV 'pathto file' -notype
You have a scope issue here. $newline has no context outside the function. Therefore you would just be adding $null to the $report array. Make the function return the value which can then be captured.
Function CreateNewLine
{
$lineproperties = #{
Cluster = $cluster
Node = $node
Database = $d.Name
LogCount = $logcount
LogPath = $p
}
New-Object PSObject -property $lineproperties
}
# Loop to create values for $cluster etc...
$report += CreateNewLine
The function should have access to those other variables as long as they are in the parent scope of the function.
The function CreateNewLine never returns a value. You need to do the following:
Function CreateNewLine
{
$lineproperties = [PSCustomObject]#{
Cluster = $cluster
Node = $node
Database = $d.Name
LogCount = $logcount
LogPath = $p
}
$lineProperties
}
You can create an object in much easier manner (as Matt said, this works in Powershell 3.0 and later):
Function CreateNewLine
{
[pscustomobject]#{
Cluster = $cluster
Node = $node
Database = "name"
LogCount = $logcount
LogPath = $p
}
}
Then, in any place you want you can use this function:
$cluster = "cluster 1"
$report += createnewline
$cluster = "cluster 2"
$report += createnewline
$report | Export-CSV 'pathto file' -notype

PowerShell keeps throwing an exception

$RootPath = "W:\"
$OutFile = "I:\Permissions.csv"
$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"
#Del $OutFile
Add-Content -Value $Header -Path $OutFile
$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}
foreach ($Folder in $Folders)
{
$ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access }
Foreach ($ACL in $ACLs)
{
$objSID = New-Object System.Security.Principal.SecurityIdentifier($ACL.IdentityReference.Value)
#$objUser = $objSID.Translate([System.Security.Principal.NTAccount])
$objUser = $objSID.Translate([System.Security.Principal.SecurityIdentifier])
$objUser.Value
$DName = ([adsi]"LDAP://<SID=$($ACL.IdentityReference.value)>").distinguishedName
$s = $DName
$s -replace "(CN=)(.*?),.*",'$2'
$dname.split("=")[1].split(",")[0]
#Show User
Write-Host “`r`nThe user mapped to SID $($objSID) is $($objUser.value)`r`n” -f “Red”
$OutInfo = $Folder.Fullname + "," + $dname.split("=")[1].split(",")[0] + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
Add-Content -Value $OutInfo -Path $OutFile
}
}
I get the following error messages.
You cannot call a method on a null-valued expression.
At I:\PermissionExporter.ps1:31 char:57
+ $OutInfo = $Folder.Fullname + "," + $dname.split <<<< ("=")[1].split(",")[0] + ","
+ $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.P
ropagationFlags
+ CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Does anyone have any idea whats going on here?
$DNName isn't doing what you expect. Add a breakpoint and verify that this:
$dname.split("=")[1].split(",")[0]
is working correctly.

Script Exporting Policies and Conditions

I would like to automatically script out all SQL Server 2008 policies and conditions on a server each night and compare the files to my version control system. In the UI, I can script out individual policies by right-clicking the policy and selecting Export Policy. Is it possible to script out policies and conditions via SMO or PowerShell?
Ideally, I would like to incorporate this into my existing PowerShell script that generates scripts for all of my other server and database objects. Here's the script that currently does this action:
# Load needed assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMOExtended")| Out-Null;
#Specify target server and databases.
$sql_server = "SomeServerName"
$SMOserver = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList "$sql_server"
$databases = $SMOserver.Databases
$BaseSavePath = "T:\SomeFilePath\" + $sql_server + "\"
#Remove existing objects.
Remove-Item $BaseSavePath -Recurse
#Script server-level objects.
$ServerSavePath = $BaseSavePath
$ServerObjects = $SMOserver.BackupDevices
$ServerObjects += $SMOserver.Endpoints
$ServerObjects += $SMOserver.JobServer.Jobs
$ServerObjects += $SMOserver.LinkedServers
$ServerObjects += $SMOserver.Triggers
foreach ($ScriptThis in $ServerObjects | where {!($_.IsSystemObject)})
{
#Need to Add Some mkDirs for the different $Fldr=$ScriptThis.GetType().Name
$scriptr = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver)
$scriptr.Options.AppendToFile = $True
$scriptr.Options.AllowSystemObjects = $False
$scriptr.Options.ClusteredIndexes = $True
$scriptr.Options.DriAll = $True
$scriptr.Options.ScriptDrops = $False
$scriptr.Options.IncludeHeaders = $False
$scriptr.Options.ToFileOnly = $True
$scriptr.Options.Indexes = $True
$scriptr.Options.Permissions = $True
$scriptr.Options.WithDependencies = $False
<#Script the Drop too#>
$ScriptDrop = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver)
$ScriptDrop.Options.AppendToFile = $True
$ScriptDrop.Options.AllowSystemObjects = $False
$ScriptDrop.Options.ClusteredIndexes = $True
$ScriptDrop.Options.DriAll = $True
$ScriptDrop.Options.ScriptDrops = $True
$ScriptDrop.Options.IncludeHeaders = $False
$ScriptDrop.Options.ToFileOnly = $True
$ScriptDrop.Options.Indexes = $True
$ScriptDrop.Options.WithDependencies = $False
<#This section builds folder structures. Remove the date folder if you want to overwrite#>
$TypeFolder=$ScriptThis.GetType().Name
if ((Test-Path -Path "$ServerSavePath\$TypeFolder") -eq "true") `
{"Scripting Out $TypeFolder $ScriptThis"} `
else {new-item -type directory -name "$TypeFolder"-path "$ServerSavePath"}
$ScriptFile = $ScriptThis -replace ":", "-" -replace "\\", "-"
$ScriptDrop.Options.FileName = $ServerSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql"
$scriptr.Options.FileName = $ServerSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql"
#This is where each object actually gets scripted one at a time.
$ScriptDrop.Script($ScriptThis)
$scriptr.Script($ScriptThis)
} #This ends the object scripting loop at the server level.
#Script database-level objects.
foreach ($db in $databases)
{
$DatabaseObjects = $db.ApplicationRoles
$DatabaseObjects += $db.Assemblies
$DatabaseObjects += $db.ExtendedStoredProcedures
$DatabaseObjects += $db.ExtendedProperties
$DatabaseObjects += $db.PartitionFunctions
$DatabaseObjects += $db.PartitionSchemes
$DatabaseObjects += $db.Roles
$DatabaseObjects += $db.Rules
$DatabaseObjects += $db.Schemas
$DatabaseObjects += $db.StoredProcedures
$DatabaseObjects += $db.Synonyms
$DatabaseObjects += $db.Tables
$DatabaseObjects += $db.Triggers
$DatabaseObjects += $db.UserDefinedAggregates
$DatabaseObjects += $db.UserDefinedDataTypes
$DatabaseObjects += $db.UserDefinedFunctions
$DatabaseObjects += $db.UserDefinedTableTypes
$DatabaseObjects += $db.UserDefinedTypes
$DatabaseObjects += $db.Users
$DatabaseObjects += $db.Views
#Build this portion of the directory structure out here. Remove the existing directory and its contents first.
$DatabaseSavePath = $BaseSavePath + "Databases\" + $db.Name
new-item -type directory -path "$DatabaseSavePath"
foreach ($ScriptThis in $DatabaseObjects | where {!($_.IsSystemObject)})
{
#Need to Add Some mkDirs for the different $Fldr=$ScriptThis.GetType().Name
$scriptr = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver)
$scriptr.Options.AppendToFile = $True
$scriptr.Options.AllowSystemObjects = $False
$scriptr.Options.ClusteredIndexes = $True
$scriptr.Options.DriAll = $True
$scriptr.Options.ScriptDrops = $False
$scriptr.Options.IncludeHeaders = $False
$scriptr.Options.ToFileOnly = $True
$scriptr.Options.Indexes = $True
$scriptr.Options.Permissions = $True
$scriptr.Options.WithDependencies = $False
<#Script the Drop too#>
$ScriptDrop = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver)
$ScriptDrop.Options.AppendToFile = $True
$ScriptDrop.Options.AllowSystemObjects = $False
$ScriptDrop.Options.ClusteredIndexes = $True
$ScriptDrop.Options.DriAll = $True
$ScriptDrop.Options.ScriptDrops = $True
$ScriptDrop.Options.IncludeHeaders = $False
$ScriptDrop.Options.ToFileOnly = $True
$ScriptDrop.Options.Indexes = $True
$ScriptDrop.Options.WithDependencies = $False
<#This section builds folder structures. Remove the date folder if you want to overwrite#>
$TypeFolder=$ScriptThis.GetType().Name
if ((Test-Path -Path "$DatabaseSavePath\$TypeFolder") -eq "true") `
{"Scripting Out $TypeFolder $ScriptThis"} `
else {new-item -type directory -name "$TypeFolder"-path "$DatabaseSavePath"}
$ScriptFile = $ScriptThis -replace ":", "-" -replace "\\", "-"
$ScriptDrop.Options.FileName = $DatabaseSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql"
$scriptr.Options.FileName = $DatabaseSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql"
#This is where each object actually gets scripted one at a time.
$ScriptDrop.Script($ScriptThis)
$scriptr.Script($ScriptThis)
} #This ends the object scripting loop.
} #This ends the database loop.
You have a couple of choices from SMO/Powershell.
1: SQLPS/PowerShell with SQL loaded
SQLSERVER:\SQLPolicy\\DEFAULT\Policies
you can then dig through it, i didnt see an "export" but you can certianly get the info out of it.
2: SMO
basically SMO has a Microsoft.SqlServer.Management.DMF namespace that has severial policy objects (what you end up with in the PowerShell side of things) Policy, PolicyStore, PolicyCondition etc, rather than write out an example, you can find one here.
http://rdbmsexperts.com/Blogs/archives/295
again i didnt see an "export" method anywhere, but you could probably spit out what you needed easily enough.