We’re in the middle of migrating from a single ConfigMgr site to having two separate sites for servers and desktops. Along with test sites, that’s a lot of sites to manage! When you’re running Powershell on a machine that is managed by a site, you can easily cd
, set-location
, or push-location
to that site’s drive. But what if you want to manage a site different than what’s managing your machine? You can open a powershell terminal or ISE session directly from the console, but that can be a hassle, and also won’t work for things not run interactively. I’ve taken to putting this code at the top of all of my scripts.
# Set up Connection $SiteCode = "" # Site code goes here $SiteServer = "" # SMS Provider (probably the site server) machine name goes here try { Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) { New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $SiteServer } Push-Location $sitecode`: } catch { $_.Exception.Message return }
What’s going on here is pretty simple. First we load the module (pulling the location from an environment variable rather than using a hard coded value). Next we check if a CMSite PSDrive exists for the specified site, and if not creates it. Finally we Push-location
to that drive. If any of those steps fail, we’ll display the error and stop executing the script.