Issue with New-DFSNFolderTarget cmdlet - powershell

I'm trying to automate repointing DFS folders. This is my current script:
$ft=get-dfsnfolder "\\test.dfs.domain\testpath\zzTop\*" | get-dfsnfoldertarget
$ft|% {
if ($_.targetpath -like "*blah*") {
new-dfsnfoldertarget -Path $_.path -State online -TargetPath ($_.targetpath).replace("\\blah.test.domain\testpath\zzTop","\\new.test.domain\testpath\zzTop")
}
}
I'm receiving a very weird error that is completely non-specific and makes this basically impossible to debug below:
new-dfsnfoldertarget : Can't add DFS namespace folder target "\\blah.test.domain\testpath\zzTop\2two"
At C:\Users\my_username\Desktop\ChangeDFSTargetTEST.ps1:2 char:46
+ ... *blah*") {new-dfsnfoldertarget -Path $_.path -State online -TargetP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_DfsNamespaceFolderTarget:ROOT\Microsoft\...aceFolderTarget) [New-DfsnFolderTarget], CimException
+ FullyQualifiedErrorId : Windows System Error 80,New-DfsnFolderTarget
new-dfsnfoldertarget : A general error occurred that is not covered by a more specific error code.
At C:\Users\my_username\Desktop\ChangeDFSTargetTEST.ps1:2 char:46
+ ... *blah*") {new-dfsnfoldertarget -Path $_.path -State online -TargetP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_DfsNamespaceFolderTarget:ROOT\Microsoft\...aceFolderTarget) [New-DfsnFolderTarget], CimException
+ FullyQualifiedErrorId : MI RESULT 80,New-DfsnFolderTarget
I'm running this script as admin on the box that is hosting and managing the DFS, so no issues with remote connections. I also have a script to offline these paths which works fine and also uses the "$_.targetpath -like "blah".
Also, if I run the command:
new-dfsnfoldertarget -Path $_.path -State online -TargetPath ($_.targetpath).replace("\\blah.test.domain\testpath\zzTop","\\new.test.domain\testpath\zzTop")
Replacing the variables with the strings that are fed into it automatically (so running it manually), it works fine. This is from a Server 2019 Standard box with PSVersion 5.1.17763.1490.
Any insight would be appreciated.

This won't be terribly relevant, but I realized I had set up DFS using an admin share path which is locked down almost completely so the testing I was doing was useless.
This script works fine if your DFS is healthy.

Related

PowerShell PackageManagement - How to uninstall a package provider?

Troubleshooting i tried uninstalling nuget. NuGetProvider-2.8.5.208.dll to be specific.
I have never worked with Power Shell before and am totally lost.
I found this question that helped me find nuget and create a command to delete it.
But I'm unable to follow through.
I did start powershell as administrator.
Here the input:
(Get-PackageProvider|where-object{$_.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
The error message:
Remove-Item : The input object cannot be bound to any parameters of the command because the command does not accept pipeline input or the input and its properties do not match any of the parameters that accept pipeline input.
and its properties do not match any of the parameters that accept pipeline input.
line:1 character:70
+ ... _.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\XXX ...GetProvider.dll:String) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.RemoveItemCommand
How can i solve this and successfully uninstall it?
Thanks for your answer!
Still no luck. Here the error:
Remove-Item : The element C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
\Microsoft.PackageManagement.NuGetProvider.dll cannot be removed: Access to the path "C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvide
r.dll" denied.
Line:1 Character:44
+ (Get-PackageProvider NuGet).ProviderPath | Remove-Item -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\XXX ...GetProvider.dll:FileInfo) [Remove-Item], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

Powershell not always running ScriptsToProcess on import

After using Install-Module to install powershell-yaml I had about a week of use before I came in one morning to it not loading YamlDotNet properly.
Now if I try to use ConvertFrom-Yaml before manually running Import-Module powershell-yaml I get the the following error:
PS C:\Users\user> "---" | ConvertFrom-Yaml
New-Object : Cannot find type [YamlDotNet.RepresentationModel.YamlStream]: verify that the assembly containing this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:24 char:23
+ ... $yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:25 char:9
+ $yamlStream.Load([System.IO.TextReader] $stringReader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Previously (and on most systems) this worked fine. If I run a manual Import-Module before calling the function it works fine:
PS C:\Users\user> Import-Module powershell-yaml
PS C:\Users\user> "---" | ConvertFrom-Yaml
PS C:\Users\user>
Both my and a coworker's machines started this behavior at about the same time. This morning my system started working properly again, but his is still exhibiting the behavior. We haven't been able to duplicate it on other machines.
I've narrowed it down to a script referenced in Powershell-Yaml manifest's ScriptsToProcess not being called during autoload, but runs fine during a manual Import-Module. On working machines the script in ScriptsToProcess is run in both cases. As a workaround we can force a module load by putting an Import-Module in our profile, ideally we'd like to find a root cause.
Import-Module -Verbose doesn't help, because calling Import-Module first always works.

Can't remove some powershell commands

I thought I'd go this sorted out in this other question but I dont seem to be able to disable the Reset-BC command.
Normally I would use
Remove-Item -force alias:Reset-BC
However I just get the following error
Remove-Item : Cannot find path 'Alias:\Reset-BC' because it does not exist.
At line:1 char:1
+ Remove-Item -force alias:\Reset-BC
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Alias:\Reset-BC:String) [Remove-Item], ItemNot
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Could anyone explain this or tell me how to get rid of it?
Definitely the thing that annoys me about powershell the most - it just litters your path with loads of rubbish. On occasion it also actually hides proper applications like where and sc with its stuff aswell which is even worse. I would use cmd but the lack of autocomplete is painful :(
You can test existance of Cmdlet:
If(get-item alias:Reset-BC) {Remove-Item -force alias:Reset-BC} else {'not exist'}

Powershell Hyper-V Scripts won't work? + Auto Installing Windows

So I have a strange problem. When I do commands such as this:
New-VM -Name "MattTest" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName Lab -path c:\VMdocs -NewVHDSizeBytes 40GB –NewVHDPath c:\base.vhdx
Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
Start-VM -Name MattTest
It works fine, However if I make a script I get a TON of errors, for example the exact same thing in a script called PS.ps1 and ran through Powershell I get:
New-VM : The parameter is not valid. No switch can be found by given
criteria. At C:\Users\mrsmith\Desktop\PS.ps1:1 char:1
+ New-VM -Name "MattTest" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName Lab - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Lab:String) [New-VM], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVMCommand
Set-VMDvdDrive : The parameter is not valid. Hyper-V was unable to
find a virtual machine with name MattTest. At
C:\Users\mrsmith\Desktop\PS.ps1:3 char:1
+ Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MattTest:String) [Set-VMDvdDrive], VirtualizationInvalidArgumentExcept ion
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMDvdDriveCommand
Set-VMDvdDrive : The parameter is not valid. No DVD drives are found.
At C:\Users\mrsmith\Desktop\PS.ps1:3 char:1
+ Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VMDvdDrive], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMDvdDriveCommand
Start-VM : The parameter is not valid. Hyper-V was unable to find a
virtual machine with name MattTest. At
C:\Users\mrsmith\Desktop\PS.ps1:5 char:1
+ Start-VM -Name MattTest
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MattTest:String) [Start-VM], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.StartVMCommand
Im not sure what's causing this? is it something to do with the module, because I have checked and the Hyper-V cmdlet IS running (considering I can manually type these in and they will be fine)
Also sidequestion: These set of instructions will get up to the "Install Windows XXXX" Screen. Is there any way to Automate THAT process? Or not really (Like make it click next, select a hard drive, install, etc...
Just to make sure, the prompt (e.g 'PS C:\Users\mrsmith\Desktop>') is not a part of the script, right? If so remove it.
As it appears, it is a part of the script because PS is an alias for Get-Process and it tries to pass 'C:\Users\mrsmith\Desktop' as a process name.

Powershell stop-service error: cannot find any service with service name

I'm working on a script to deploy vendor software to a large environment. The first step is to stop the services in question. The script executes fine in our test environment, but I'm not an admin in the production environment so I'm convinced it's a permissions issue. I can't get admin rights to the prod environment so I need to try to find out anything that I may need to set to grant permissions to stop services remotely. I'm issuing the following command to stop services:
Stop-Service -InputObject $(Get-Service -Computer $destination.Server -Name ("moca."+$destEnv))
When I run the script I get:
Cannot find any service with service name 'moca.WMSPRD'.
+ CategoryInfo : ObjectNotFound: (moca.WMSPRD:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StopServiceCommand
The service definitely exists and if I rdp into the target box and issue the stop-service command locally it will execute. So there is something preventing me from stopping the service remotely. Any ideas?
Edit:
A coworker suggested using WMI so tried replacing the Stop-Service line with:
(Get-WmiObject -computer $destination.Server Win32_Service -Filter ("Name='moca."+$destEnv+"'")).InvokeMethod("StopService",$null)
and I get:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
If you know the exact service name you can try this
(Get-WmiObject -computerName $_.name Win32_Service -Filter "Name='moca'").StopService()
Here im assuming that the service name is moca
Is DCOM working on the remote computer? I know how to do it with remote powershell, which uses wsman:
invoke-command comp001 { stop-service adobearmservice }