Unknown's avatar

About David das Neves

Hi. My name is David and I work as Premier Field Engineer at Microsoft Germany. My specialties are Dev, Powershell and Windows 10 Client and I am focused in client workshops and dev requests. Beside my work I write my own blog and speak at conferences on PS/Dev topics. Feel free to contact me.

PS – DFS-Export

Hello all,

to export DFS-namespaces the tool dfsutil can be used.

Here is an simple example herefore.

$DFSRoots = (Get-DFSNRoot).Path
$Date = Get-Date -Format yyy-MM-dd
ForEach ($Root in $DFSRoots)
    { 
       dfsutil root export $($Root) $("D:\DFSBCK-$Root-$Date.xml")
    }

~David

PS – Retrieve desktop content of many computers

Hello all,

here i have an example which shows how to get the desktop content from many computers.
A nice feature here is that it gets the desktops of the signed on users. So you won´t grab unnecessary contents.

$Computers = 'localhost'

foreach ($Computer in $Computers){

    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | Where-Object{$_ -notlike "*DWM*"} | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}

This logic can also be modified easily for other purposes.

Have fun with it!

~David

PS – LoggedOnUser

Hello all,

the following code shows how to gather the actual logged on user. This snippet can be integradted easily in other scripts.


Get-WmiObject win32_logonsession -ComputerName $comp -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
foreach {Get-WmiObject win32_loggedonuser -ComputerName $comp -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } |  Where-Object{$_ -notlike "*DWM*"} | select -unique

~David

Windows Client – failing Updates

Hello together,

i have worked in a 3rd level support desk where we had to fight against many errors with Windows updates. By this time i worked out a script which fixed nearly all of the errors at the first try.

The DISM commands are only availlable on Windows 8 / 10 devices.
Just copy the whole script into a batch file and execute it as administrator.

DISM /Online /Cleanup-Image /CheckHealth
DISM /Online /Cleanup-Image /ScanHealth
DISM /Online /Cleanup-Image /RestoreHealth
DISM /Online /Cleanup-Image /ScanHealth

sfc /scannow
findstr /c:"[SR]" %windir%\logs\cbs\cbs.log > c:\windows\logs\cbs\sfcdetails.log

net stop wuauserv
net stop cryptSvc
net stop bits
net stop msiserver
ren %WINDIR%\SoftwareDistribution SoftwareDistribution.bak
ren %WINDIR%\System32\catroot2 catroot2.bak
net start wuauserv
net start cryptSvc
net start bits
net start msiserver

fsutil resource setautoreset true c:\
echo #### Info:
fsutil resource info C:

echo MSI
sc config msiserver start= demand
Net stop msiserver
MSIExec /unregister
MSIExec /regserver
regsvr32.exe /s %windir%\system32\msi.dll
Net start msiserver
sc config msiserver start= auto

shutdown /g /t 60

In c:\windows\logs\cbs\sfcdetails.log the log of the sfc is stored. It is always good to throw an eye into this log. Sometimes trivial errors are visible in here.

After the usage of the script the computer has to be rebooted twice. This is an experience value because of the sfc and the fsutil command.

If the error persists you have also more steps you can do. One of the first things should be to do a clean boot. Some programs intervent the update process – for example firewall, anti virus etc. If it gets little harder you should analyze the cbs.log which is stored under c:\windows\logs\CBS\cbs.log and also the DISM log (if Win 8 or 10) stored in c:\windows\logs\DISM.

You can gather also some information in the eventlogs. Herefore open the eventviewer and the tab “Installation”. All elements with the ID “3” are errors.

If you use still Windows 7 you have the option to try also the CheckSUR. This “Hotfix” will prove all installed updates and try to repair them. You can find it here.
Be sure that you download the correct version for your OS and let it run. It may take several time up to some hours. When its finished it will also write a log-file in c:\windows\logs\cbs\checksur.log.
You should verify it to get more hints what the main problem is.

If you find any errors you can resolve them like specified here

Good look and please prevent me with feedback if you were lucky using this methode.

~David

PS – parameter by reference

Many developers know them – parameter by reference. By this way the variables will not be duplicated. A pointer is created which points to the actual variable. If you modify this referenced parameter you will modify the referenced variable. So you can make changes to many parameters and will not create a lot of functions with returnvalues.

[string]$Global = ''


function TestRefFunction([ref]$Param)
{
    [string]$Param.Value = '5'
    $Param | Out-Default
}

TestRefFunction([ref]$Global)

$Global | Out-Default

By this example the transmitted parameter “$global” is a reference. So all changes to it in the function will also be written into the global variable. In this example so 2 times a “5” will be given out.

~David

PS – bulk-import of AD-Users with skipping blank values

In the following example an bulk-import of AD-Users is done. The clever thing – blank parameters will be skipped. A condition for this to work properly is that the the headers of the csv-file must match the names in the AD.

Import-Module ActiveDirectory
$users = Import-Csv -Path C:\temp\export\workspace.csv
foreach ($user in $users) {
    $properties = @{}
    $user | Get-Member -Type NoteProperty | Select -Expand Name | Foreach {
        if ($user."$_") { $properties.Add("$_", $user."$_") }
    }
    Set-ADUser -Identity $user.sAMAccountName @properties -Verbose
}

~David

PS – configuration for bigger scripts and simple logging

Hello all

In this post i want to show you how to create a configuration with a XML-file easily.
Lots of configuration parameters can be placed in here so that the end user makes his changes in this XML-file where the impact of modifications can be mitigated. Also it cleans up the source code. By loading all relevant parameters within one file you have to look and store all properties at only one place.

Also you can place complex configuration lists and conditional matrixes in it to load them later on with powershell and filter the outcome.

Some examples:

Loading the XML-file

[xml] $configXml = Get-Content -Path "$PSScriptRoot\Config.xml" -ErrorAction 'Stop'

Yeah – that easy.

Exemplaric structure of a file:

<?xml version="1.0" standalone="yes"?>
<Config>
   <DBConnectionString>Provider = OraOLEDB.Oracle; Data Source = ds ; User Id = USER ; Password = pwuser; OLEDB.NET = True;</DBConnectionString>
   <LogDir>c:\Logs</LogDir>
   <ConfigValue Value1="val1"></ConfigValue>
   <ConditionalMatrix>
     <Parameter Value1="val1" Value2="start" BoolValue="1"></Parameter>
     <Parameter Value1="val1" Value2="end" BoolValue="0"></Parameter>
     <Parameter Value1="val2" Value2="test" BoolValue="1"></Parameter>
     <Parameter Value1="val2" Value2="test" BoolValue="0"></Parameter>
     <Parameter Value1="val3" Value2="prod" BoolValue="1"></Parameter>
     <Parameter Value1="val3" Value2="test" BoolValue="0"></Parameter>
   </ConditionalMatrix>
</Config>

Retrieving the data:

#fetching the data directly
$ConnectionString= $configXML.Config.DBConnectionString
$Value1= $configXML.Config.ConfigValue.Value1

$Value1ByConditionalMatrix= ($configXML.Config.ConditionalMatrix.Parameter  | Where-Object {$_.Value2-eq "test" -and $_.BoolValue-eq "1"}).Value1

Easy isn´t it?! Now you have to load the configuration within the script from a relativ path. This could also be the path where afterwards the loggings could be stored into.

A very simple way for logging is to set gather the logging path from config/relative and write log files with a date in it. So you get an overview for each day.

$LogDir=$configXML.Config.LogDir
$LogFile = "$LogDir\Log" + ((Get-Date).Day) + "_" + ((Get-Date).Month) + "_" + ((Get-Date).Year) + "_" + ((Get-Date).Hour) + "_" + ((Get-Date).Minute) +".log"
(Get-Date).ToString()  + " Something happened $Object" >> $LogFile

This can also be empowered even more if you build a separate function for this. So you can grab executing function, date, user etc. which will be written to a log file.

I hope i could give you some hints of the power with working xml-configuration files and enabling you the view for very simple logging.

~David

PS – loading powershell cmdlets for SCCM

If you have SCCM in use you should also throw an eye into the specific powershell cmdlets.
To use them you must previously load them with “Import-Module”. Use also a dynamic environment variable to do this so you can copy it easily from one computer to another without resetting the specific path.

Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)

Exemplaric function:

Get-CMDevice -CollectionName "Win 10 Desktops"

There are up to now a nearly uncountable bunch of cmdlets. You can do nearly everything with these powershell cmdlets in powershell. By automizing some processes you will reduce the errors by human inputs and also increase the speed of this processes.

~David

PS – download of csv-files of a folder from Sharepoint via webclient

In this example the assumption is that you don´t have a Sharepoint server on which you could use the Sharepoint commandlets. So only the standard webclient functions were used to allow you to download all files. Also you need some valid credentials to log onto the Sharepoint website otherwise you would get the error 401 (not authorized).

Here is the code herefore:


<# 
.Synopsis 
Download all files from a sharepoint folder. 
.DESCRIPTION 
download all files from a sharepoint aspx-site. 
.EXAMPLE 
$credentials=Get-Credential -Credential Domäne\Benutzer 
[string]$sharepointURL = 'https://domäne.de/ExportOrdner/Forms/Overview.aspx' Download-AllFilesFromSP -SP_ASPX_Page $sharepointURL -DestinationFolder 'C:\ExportDirectory' -Credentials $credentials 
.INPUTS SP_ASPX_Page - aspx-site DestinationFolder target  CredentialsSP - authentication 
.FUNCTIONALITY 
Web; Sharepoint; Download 
#>
  function Download-AllCSVFilesFromSP
        {
            PARAM (
                [parameter(Mandatory=$true)]
                $SP_ASPX_Page,
                [parameter(Mandatory=$true)]
                $DestinationFolder,
                [parameter(Mandatory=$true)]
                $CredentialsSP
            )

            #create Object            
            $webclient = New-Object System.Net.WebClient 

            #set credentials
            $webclient.Credentials = $CredentialsSP 
            
            #retrieve content
            $response = Invoke-WebRequest -Uri $SP_ASPX_Page -Credential $CredentialsSP -Method Get

            #parse content parsen and search for export-files
            $matches =[regex]::Matches($response.content,'(\sUrl=\")+[0-9,a-z,A-Z\/_-]*(.csv\"\s)') 

            #foreach found file
            foreach ($item in $matches)
            {
                    #clean the download string             
                    $downloadFileURL = ($item.Groups[0].Value).Split('"')[1]

                    #extract the domain
                    $UrlDomain = ([regex]::Matches($SP_ASPX_Page,'https://[0-9,a-z,A-Z_\-\.]*')).Value

                    #extract filename
                    $downloadFile = $downloadFileURL.Split('/')[-1]
                    
                    #download to destination folder
                    $webclient.downloadfile($UrlDomain + $downloadFileURL,  $DestinationFolder + '\' + $downloadFile) 
            }
        }

PS – remote execution of scripts

In this post i want to show you some ways how to execute remote execution of scripts.

First the complicated way:

connect – set execution policy – copy script, execute, delete – roll back execution policy

$Computername=Name
#"Start Session"
$s = New-PSSession -ComputerName $Computername
#"Enable Script Execution on remote System..."
Invoke-Command -Session $s -scriptblock { Set-executionpolicy unrestricted }
#Copy Script
Copy-Item H:\Skript.ps1 \\$Computername\c$\temp\Skript.ps1
#"Start Script..."
Invoke-Command -Session $s -scriptblock { c:\temp\Skript.ps1 }
#Delete Script
Remove-Item \\$Computername\c$\Skript.ps1
#Disable Script Execution on remote System...
Invoke-Command -Session $s -scriptblock { Set-executionpolicy default }
#End Session
Remove-PSSession $s

Next we use Invoke-Command – the scriptfile is copied and executed within the command itself:

Invoke-Command -computer Computername -FilePath C:\Skript.ps1

Next code connects to a computer NOT using Kerberos. This can become necessary if you want to connect to computers outside your domain:

$Computername=Name

#Not using Kerberos:
cd WSMan:\localhost\Client
Set-Item trustedhosts $Computername -force
Restart-Service winrm
Invoke-Command Computername { #Skript in here#} -authentication negotiate -credential Get-Credential

Have fun with it.

~David