i trying take backup of sql server content database using powershell.
here code:
$dt = get-date -format yyyymmddhhmmss $null = [reflection.assembly]::loadwithpartialname("microsoft.sqlserver.smo"); $srvrvinstancename= "srvr-04\mssqlserver1" $svr = new-object microsoft.sqlserver.management.smo.server($srvrvinstancename); #write-output $svr wss_content_2d426b353ded49aa8ace920d4178c298 $bdir = "c:" $svnm = $svr.name $db = $svr.databases['wss_content_2d426b353ded49aa8ace920d4178c298'] $dbname = $db.name $dt = get-date -format yyyymmddhhmmss $bfil = "$bdir\$($dbname)_db_$($dt).bak" backup-sqldatabase -serverinstance $svnm -database $dbname - backupfile $bfil
but when run code error:
backup-sqldatabase : 'backup-sqldatabase' command found in module 'sqlps', module not loaded. more information, run 'import-module sqlps'.
@ line:19 char:1
+ backup-sqldatabase -serverinstance $svnm -database $dbname -backupfile $bfil + ~~~~~~~~~~~~~~~~~~ + categoryinfo : objectnotfound: (backup-sqldatabase:string) [], commandnotfoundexception + fullyqualifiederrorid : couldnotautoloadmatchingmodule
after that, ran
import-module sqlps
but ran error state:
import-module : file c:\program files (x86)\microsoft sql server\110\tools\powershell\modules\sqlps\sqlps.ps1 cannot loaded because running scripts disabled on system.
more information, see about_execution_policies @ http://go.microsoft.com/fwlink/?linkid=135170.
@ line:1 char:1
+ import-module sqlps
+ ~~~~~~~~~~~~~~~~~~~ + categoryinfo : securityerror: (:) [import-module], pssecurityexception + fullyqualifiederrorid : unauthorizedaccess,microsoft.powershell.commands.importmodulecommand
what doing wrong?
i think need load couple off assembly:
# load sql server assemblies # [system.reflection.assembly]::loadwithpartialname("microsoft.sqlserver.smo") | out-null # smoextended # [system.reflection.assembly]::loadwithpartialname("microsoft.sqlserver.smoextended") | out-null [reflection.assembly]::loadwithpartialname("microsoft.sqlserver.connectioninfo") | out-null [reflection.assembly]::loadwithpartialname("microsoft.sqlserver.smoenum") | out-null
update.
for scripts can't executed need (as administrator in powershell)
set-executionpolicy remotesigned
Comments
Post a Comment