function ChangeFolderOwner([string]$user, [string]$folderpath)
{
<#
.SYNOPSIS
Меняет у папки владельца

.DESCRIPTION

.PARAMETER user 
Пользователь - новый владелец

.PARAMETER folderpath 
Путь к папке

.EXAMPLE 
ChangeFolderOwner -folderpath:"C:\ProgramData\MySQL\MySQL Server 5.6\data\kukin2" -user:"SDC\kachalkin"


.NOTES
#>
$acl = Get-Acl $folderpath
$acl.SetOwner([System.Security.Principal.NTAccount]$User)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($User,"FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
$acl | Set-Acl $folderpath
}

function RemotlyChangeFolderOwner([string]$user, [string]$folderpath, [string]$serverName)
{
	Write-Host ($CHANGE_OWNER_START -f $folderpath)
	Invoke-Command -ComputerName $serverName -ScriptBlock ${function:ChangeFolderOwner} -ArgumentList $user, $folderpath;
	Write-Host ($CHANGE_OWNER_END -f $folderpath) -ForegroundColor Yellow
}

function GetFolderSize([string]$directory,[string]$server)
{
<#
.SYNOPSIS
Возвращает размер папки с указанным именем на указанном сервере

.DESCRIPTION

.PARAMETER directory 
Путь к папке

.PARAMETER server 
IP-адрес или DNS-имя сервера

.EXAMPLE 
GetFolderSize -server "iis.server.local" -directory "C:\Scripts"

.NOTES
#>
	# $PasswordObj = ConvertTo-SecureString –String $password –AsPlainText -Force -ErrorAction Stop;
	# $UserCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $login,$PasswordObj -ErrorAction Stop;

	Write-Host ($MSG_FOLDER_SIZE_GETTING -f $directory,$server);
	$ExitObj = Invoke-Command -ComputerName $server -ScriptBlock {
		param
		(
			[string]$directory
		)
		try
		{
			$size = (Get-ChildItem $directory -Recurse | Where-Object {$_.PSObject.Properties.Match('Length')} |  Measure-Object -property Length -Sum).Sum / 1MB;
			if($size -eq $null){ return 0; }
			else { return $size; }
		}
		catch
		{
			$returnedException = New-Object PSObject;
			Add-Member -InputObject $returnedException -MemberType NoteProperty -Name Message -Value $_.Exception.Message;
			Add-Member -InputObject $returnedException -MemberType NoteProperty -Name ExceptionType -Value $_.Exception.GetType().FullName;
			return $returnedException;
		}
		
	} -ArgumentList $directory -ErrorAction Stop;
	
	if(isNumber($ExitObj))
	{
		Write-Host ($MSG_FOLDER_SIZE_GETTED -f $directory,$server,$ExitObj);
		return $ExitObj;
	}
	else
	{
		throw (CreateException -exInfo $ExitObj);
	}
}

filter isNumber($num)
{
	return $num -is [byte]  -or $num -is [int16]  -or $num -is [int32]  -or $num -is [int64]  `
       -or $num -is [sbyte] -or $num -is [uint16] -or $num -is [uint32] -or $num -is [uint64] `
       -or $num -is [float] -or $num -is [double] -or $num -is [decimal];
}

function CreateException($exInfo)
{
	$ex = New-Object -TypeName $exInfo.ExceptionType -ArgumentList $exInfo.Message;
	return $ex;
}